undefined reference to `pthread_create‘(linux下Clion使用thread报错)

完整报错:

====================[ Build | AsyncLogger | Debug ]=============================
/snap/clion/169/bin/cmake/linux/bin/cmake --build /home/dyy/CLionProjects/AsyncLogger/cmake-build-debug --target AsyncLogger -- -j 6
[ 50%] Linking CXX executable AsyncLogger
/usr/bin/ld: CMakeFiles/AsyncLogger.dir/main.cpp.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
/usr/include/c++/9/thread:126: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/AsyncLogger.dir/build.make:93:AsyncLogger] 错误 1
make[2]: *** [CMakeFiles/Makefile2:83:CMakeFiles/AsyncLogger.dir/all] 错误 2
make[1]: *** [CMakeFiles/Makefile2:90:CMakeFiles/AsyncLogger.dir/rule] 错误 2
make: *** [Makefile:124:AsyncLogger] 错误 2

解决方法:
在CMakeLists.txt的最后加上:

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

然后reload CMakeLists.txt,编译成功。
原因:
编译器没有链接线程库

猜你喜欢

转载自blog.csdn.net/qq_42604176/article/details/121355217