简体   繁体   中英

Link OpenSSL statically in cmake_list

I am coding an encryption program for educational purposes. I am using openssl as the encryption library. The problem is that I want to produce an executable that can run on systems without openssl installed. Therefor I need to link openssl statically. I am using a cmake_list for building.

As soon as I add the flag OPENSSL_USE_STATIC_LIBS TRUE I get linker errors... I have tried building openssl from sources and passing it to the find_package command but this still gives me a linker error for all openssl headers.

'''c++
cmake_minimum_required(VERSION 3.13)

project(encryption_test)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(OPENSSL_ROOT_DIR /usr/local/ssl)
set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)

include_directories(
    include/)
add_executable(${PROJECT_NAME}
    src/main.cpp)

target_link_libraries(${PROJECT_NAME}
    stdc++fs
    OpenSSL::SSL)
'''

The errors are like:

undefined reference to 'pthread_rwlock_init'

So many thanks for any help or ideas how I can fix this!

I actually solved the problem with an answer given in Static linking of OpenSSL Crypto in CMake

The hint target_link_libraries(program ${CMAKE_DL_LIBS}) solved my linker issues, just in case anyone else struggels with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM