我需要制作一个基本上静态的应用程序/库。此应用程序的依赖项之一是libpthread。要求我静态链接到libpthread。我正在使用CMake。
如果我要运行ldd myapp
,应该不列出libpthread.so.0
。
我尝试了多种方法以及每种方法的各种组合,所有这些都会导致动态链接:
find_package(Threads REQUIRED)
target_link_libraries(myapp
PRIVATE
Threads::Threads
)
target_link_libraries(myapp
PRIVATE
-lpthread -Wl,-u,pthread_join,-u,pthread_equal
)
target_link_libraries(myapp
PRIVATE
-Wl,--whole-archive -lpthread -Wl,--no-whole-archive
)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
set_target_properties(myapp PROPERTIES LINK_SEARCH_START_STATIC 1)
set_target_properties(myapp PROPERTIES LINK_SEARCH_END_STATIC 1)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
find_package(Threads REQUIRED)
target_link_libraries(myapp Threads::Threads)
和
target_link_libraries(myapp
PRIVATE
-static -lpthread
)