编译源代码文件,无`pthread_create’函数的定义,即未链接pthread库文件。
root@localhost:~/testWorkSpace/vector$ cmake --build ./build/
Scanning dependencies of target move
[ 50%] Building CXX object CMakeFiles/move.dir/testMove.cpp.o
[100%] Linking CXX executable move
CMakeFiles/move.dir/testMove.cpp.o: In function `std::thread::thread<int (SucDriver::*)(unsigned int), SucDriver&, int>(int (SucDriver::*&&)(unsigned int), SucDriver&, int&&)':
testMove.cpp:(.text._ZNSt6threadC2IM9SucDriverFijEJRS1_iEEEOT_DpOT0_[_ZNSt6threadC5IM9SucDriverFijEJRS1_iEEEOT_DpOT0_]+0x4b): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
CMakeFiles/move.dir/build.make:83: recipe for target 'move' failed
make[2]: *** [move] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/move.dir/all' failed
make[1]: *** [CMakeFiles/move.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
修改CMakeLists.txt文件,链接pthread库
# specify the CMAKE tool version
cmake_minimum_required(VERSION 3.14.7)
# specify the project name
project(move)
target_link_libraries(move pthread)
add_executable(move testMove.cpp)
执行命令,生成makefile编译配置文件,会出现以下报错:
root@loccalhost:~/testWorkSpace/vector$ cmake -B ./build/ -S .
CMake Error at CMakeLists.txt:7 (target_link_libraries):
Cannot specify link libraries for target "move" which is not built by this
project.
-- Configuring incomplete, errors occurred!
See also "/home/lki8szh/testWorkSpace/vector/build/CMakeFiles/CMakeOutput.log".
修改CMakeLists.txt文件
# specify the CMAKE tool version
cmake_minimum_required(VERSION 3.14.7)
# specify the project name
project(move)
add_executable(move testMove.cpp)
# 文件链接在代码编译之后
target_link_libraries(move pthread)
执行命令,生成makefile成功。
root@localhost:~/testWorkSpace/vector$ cmake -B ./build/ -S .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lki8szh/testWorkSpace/vector/build
编译成功
root@localhost:~/testWorkSpace/vector$ cmake --build ./build/
[ 50%] Linking CXX executable move
[100%] Built target move