【发布时间】:2020-04-09 09:16:12
【问题描述】:
我正在尝试静态链接多线程二进制文件。如果 CMake 可以警告缺少静态库,那就太好了。
注意:我知道解决方案是安装静态库(Fedora 31 中的 libstdc++-static 和 glibc-static,如果这对任何人都有帮助的话),我只希望 CMake 报告丢失的库和不是在构建时。
(顺便说一句,有没有更好的方法来支持静态链接?目前我有一些平台相关的东西可以在我们的目标系统中支持它,我还没有发现任何更便携的东西......)
这是一个示例 CMakeLists.txt:
cmake_minimum_required(VERSION 3.17)
project(test)
set(CMAKE_CXX_STANDARD 17)
add_link_options(-static)
find_package(Threads REQUIRED)
add_executable(test)
target_link_libraries(test PRIVATE Threads::Threads)
target_sources(test PRIVATE test.cpp)
这是输出:
$ cmake ..
-- The C compiler identification is GNU 9.3.1
-- The CXX compiler identification is GNU 9.3.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sergio/src/static_cmake/build
$ make
Scanning dependencies of target test
[ 50%] Linking CXX executable test
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test.dir/build.make:101: test] Error 1
make[1]: *** [CMakeFiles/Makefile2:93: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:101: all] Error 2
【问题讨论】:
-
find_library(libpthread.a)?
标签: c++ cmake static-libraries