undefined reference to `pthread

undefined reference to `pthread,第1张

undefined reference to `pthread

完整报错:

====================[ 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 (&)())':
/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,编译成功。
原因:
编译器没有链接线程

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5520769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存