解决在linux环境下的clion 运行不了thread的问题

问题

我们在linux下使用clion编程时,遇到这种情况

[ 50%] Linking CXX executable Study_C_project
CMakeFiles/Study_C_project.dir/main.cpp.o: In function std::thread::thread(void (&)(int), int&&)': /usr/include/c++/7/thread:122: undefined reference topthread_create’
collect2: error: ld returned 1 exit status
CMakeFiles/Study_C_project.dir/build.make:94: recipe for target ‘Study_C_project’ failed
make[3]: *** [Study_C_project] Error 1
CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/Study_C_project.dir/all’ failed
make[2]: *** [CMakeFiles/Study_C_project.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target ‘CMakeFiles/Study_C_project.dir/rule’ failed
make[1]: *** [CMakeFiles/Study_C_project.dir/rule] Error 2
Makefile:118: recipe for target ‘Study_C_project’ failed
make: *** [Study_C_project] Error 2

重点是:std::thread::thread(void (&)(int), int&&)': /usr/include/c++/7/thread:122: undefined reference to

说明了我们的thread是没有被定义的

解决方法

在CMakeLists.txt的最后加上

find_package(Threads REQUIRED)
target_link_libraries(Study_C_project Threads::Threads)Threads::Threads)	#这里Study_C_project 是你的项目名称

你可能感兴趣的:(C++)