编译一个cmake 小项目
$ mkdir build
$ cd build
$ cmake ../
$ make
Consolidate compiler generated dependencies of target cwget
[ 50%] Building CXX object CMakeFiles/cwget.dir/main.cpp.o
[100%] Linking CXX executable cwget
CMakeFiles/cwget.dir/main.cpp.o:在函数‘main’中:
main.cpp:(.text+0xbac):对‘pthread_create’未定义的引用
main.cpp:(.text+0xbc0):对‘pthread_join’未定义的引用
collect2: error: ld returned 1 exit status
CMakeFiles/cwget.dir/build.make:96: recipe for target 'cwget' failed
make[2]: *** [cwget] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/cwget.dir/all' failed
make[1]: *** [CMakeFiles/cwget.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2
出现错误, 是连接错误, 显然应该连接pthread 库.
打开CMakeLists , 在最末尾添加:
target_link_libraries(cwget pthread)
完整文件如下:
cat CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
project(cwget)
#set(CMAKE_CXX_STANDARD 11)
add_executable(cwget main.cpp)
target_link_libraries(cwget pthread)