Bug描述
在ubuntu16.04下采用CMake
编译c++
代码,在CMakeLists.txt
里的add_definitions()
已添加-lpthread
(即add_definitions("-g -std=c++11 -lpthread")
),CMakeLists.txt
文件内容如下:
cmake_minimum_required(VERSION 3.5)
project(xjc_cpp11)
set(CMAKE_CXX_STANDARD 14)
add_definitions("-g -std=c++11 -lpthread")
add_executable(xjc_cpp11
main.cpp
AlgorithmTwo.cpp
CrashIdentifier.cpp
Gps.cpp
Gsensor.cpp
Motion.cpp
Stay.cpp)
执行如下命令时仍然报错——
patten@patten-hp:~/workspace/xj_collide/xj_collide/OO_solution_cpp98/src$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src
patten@patten-hp:~/workspace/xj_collide/xj_collide/OO_solution_cpp98/src$ make
[ 12%] Linking CXX executable xjc_cpp11
CMakeFiles/xjc_cpp11.dir/main.cpp.o:在函数‘threadGps_data_capture()’中:
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:44:对‘pthread_create’未定义的引用
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:45:对‘pthread_detach’未定义的引用
CMakeFiles/xjc_cpp11.dir/main.cpp.o:在函数‘threadSensor_data_capture()’中:
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:68:对‘pthread_create’未定义的引用
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:69:对‘pthread_detach’未定义的引用
CMakeFiles/xjc_cpp11.dir/main.cpp.o:在函数‘threadStayPointDetect()’中:
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:91:对‘pthread_create’未定义的引用
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:92:对‘pthread_detach’未定义的引用
collect2: error: ld returned 1 exit status
CMakeFiles/xjc_cpp11.dir/build.make:250: recipe for target 'xjc_cpp11' failed
make[2]: *** [xjc_cpp11] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/xjc_cpp11.dir/all' failed
make[1]: *** [CMakeFiles/xjc_cpp11.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
patten@patten-hp:~/workspace/xj_collide/xj_collide/OO_solution_cpp98/src$
解决方案
由之前经验可知,采用g++
编译时会有如下情况:
- 执行
g++ -g -std=c++11 *.cpp -lpthread
编译可以通过pass - 执行
g++ -g -std=c++11 -lpthread *.cpp
编译不可以通过not-pass
如下所示:
patten@patten-hp:~/workspace/xj_collide/xj_collide/OO_solution_cpp98/src$ g++ -g -std=c++11 *.cpp -lpthread
patten@patten-hp:~/workspace/xj_collide/xj_collide/OO_solution_cpp98/src$ g++ -g -std=c++11 -lpthread *.cpp
/tmp/ccuE659E.o:在函数‘threadGps_data_capture()’中:
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:44:对‘pthread_create’未定义的引用
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:45:对‘pthread_detach’未定义的引用
/tmp/ccuE659E.o:在函数‘threadSensor_data_capture()’中:
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:68:对‘pthread_create’未定义的引用
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:69:对‘pthread_detach’未定义的引用
/tmp/ccuE659E.o:在函数‘threadStayPointDetect()’中:
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:91:对‘pthread_create’未定义的引用
/home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src/main.cpp:92:对‘pthread_detach’未定义的引用
collect2: error: ld returned 1 exit status
patten@patten-hp:~/workspace/xj_collide/xj_collide/OO_solution_cpp98/src$
所以接下来要解决的问题就是要把-lpthread
放在编译选项最后,添加如下两行即可解决问题:
find_package(Threads)
target_link_libraries (${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})
最终的CMakeLists.txt
文件为:
cmake_minimum_required(VERSION 3.5)
project(xjc_cpp11)
set(CMAKE_CXX_STANDARD 14)
find_package(Threads)
add_definitions("-g -std=c++11 -lpthread")
add_executable(xjc_cpp11
main.cpp
AlgorithmTwo.cpp
CrashIdentifier.cpp
Gps.cpp
Gsensor.cpp
Motion.cpp
Stay.cpp)
target_link_libraries (${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})
Congratulations!编译通过——
patten@patten-hp:~/workspace/xj_collide/xj_collide/OO_solution_cpp98/src$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/patten/workspace/xj_collide/xj_collide/OO_solution_cpp98/src
patten@patten-hp:~/workspace/xj_collide/xj_collide/OO_solution_cpp98/src$ make
Scanning dependencies of target xjc_cpp11
[ 12%] Building CXX object CMakeFiles/xjc_cpp11.dir/main.cpp.o
[ 25%] Building CXX object CMakeFiles/xjc_cpp11.dir/AlgorithmTwo.cpp.o
[ 37%] Building CXX object CMakeFiles/xjc_cpp11.dir/CrashIdentifier.cpp.o
[ 50%] Building CXX object CMakeFiles/xjc_cpp11.dir/Gps.cpp.o
[ 62%] Building CXX object CMakeFiles/xjc_cpp11.dir/Gsensor.cpp.o
[ 75%] Building CXX object CMakeFiles/xjc_cpp11.dir/Motion.cpp.o
[ 87%] Building CXX object CMakeFiles/xjc_cpp11.dir/Stay.cpp.o
[100%] Linking CXX executable xjc_cpp11
[100%] Built target xjc_cpp11
patten@patten-hp:~/workspace/xj_collide/xj_collide/OO_solution_cpp98/src$ ./xjc_cpp11
^C
patten@patten-hp:~/workspace/xj_collide/xj_collide/OO_solution_cpp98/src$