问题一
如何静态链接OpenSSL?
# Linux
set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)
target_link_libraries(${PROJECT_NAME} OpenSSL::Crypto OpenSSL::SSL)
# Macos
include_directories(/usr/local/Cellar/openssl@1.1/1.1.1k/include/)
target_link_libraries(decrypt /usr/local/Cellar/openssl@1.1/1.1.1k/lib/libcrypto.a)
问题二
Glibc版本不一致问题 version ‘GLIBCXX_3.4.21' not found
解决方案:
- 编译生成可执行文件
cmake_minimum_required(VERSION 3.21)
project(projectName)
# Executable
set(CMAKE_EXE_LINKER_FLAGS "-static")
set(CMAKE_CXX_FLAGS "-Wl,--whole-archive -lpthread -Wl,--no-whole-archive")
add_executable(${PROJECT_NAME} main.cpp)
或者使用
target_link_libraries(${PROJECT_NAME} -static-libgcc -static-libstdc++)
但无法解决,libc.so依旧是动态链接
- 编译生成SO动态库
# Dynamic lib
set(CMAKE_SHARED_LINKER_FLAGS "-static") // 注意Flag不一样,要区分
set(CMAKE_CXX_FLAGS "-Wl,--whole-archive -lpthread -Wl,--no-whole-archive -Bstatic -lc -fPIC")
add_library(${PROJECT_NAME} SHARED main.cpp)
该方案无法静态链接libc,原因可能是libc无法被静态链接(有大佬有解决方案也可评论下)
Error:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/10/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object