我想静态链接下面列出的库:
set_target_properties(exec PROPERTIES LINK_SEARCH_START_STATIC 1)
set_target_properties(exec PROPERTIES LINK_SEARCH_END_STATIC 1)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
find_library(SODIUM_LIB libsodium.a REQUIRED)
find_library(SSL_LIB libssl.a REQUIRED)
find_library(CRYPTO_LIB libcrypto.a REQUIRED)
find_library(DL_LIB libdl.a REQUIRED)
message(${SODIUM_LIB})
message(${SSL_LIB})
message(${CRYPTO_LIB})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
target_link_libraries(
exec
${SODIUM_LIB}
${SSL_LIB}
${CRYPTO_LIB}
${DL_LIB}
我不想将-static
添加到CMAKE_EXE_LINKER_FLAGS
,因为在这种情况下,所有内容都是静态链接的。 CMake找到静态库:
/usr/local/lib/libsodium.a
/usr/lib/x86_64-linux-gnu/libssl.a
/usr/lib/x86_64-linux-gnu/libcrypto.a
除了libcrypto
之外,似乎所有内容都是静态链接的:
readelf -d exec
0x0000000000000001 (NEEDED) Shared library: [libcrypto.so.1.0.0]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x0000000000000001 (NEEDED) Shared library: [ld-linux-x86-64.so.2]
如何将libcrypto
静态链接到我的可执行文件?
答案 0 :(得分:3)
FindOpenSSL page上从版本3.4开始的CMake文档说:
将OPENSSL_USE_STATIC_LIBS设置为TRUE以查找静态库。
(假设他们被发现将被使用)
示例:
cmake_minimum_required(VERSION 3.4)
project(Foo)
set(SOURCE_FILES main.cpp)
set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} OpenSSL::Crypto)