Linux的g++编译:undefined reference to `pthread_create‘

linux编译一个c++程序,编译报错如下:

lmh@master:~/Documents/HW$ g++ -std=c++11 condi_test1.cpp -o 1-demo
/tmp/ccBCiMQ5.o: In function `std::thread::thread<void (&)()>(void (&)())':
condi_test1.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEEEOT_DpOT0_]+0x7d): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

原因:pthread不是linux下的默认的库,也就是在链接的时候,无法找到phread库中哥函数的入口地址,于是链接会失败。

解决:编译命令后,附加 -lpthread 参数。

g++ -std=c++11 condi_test1.cpp -o 1-demo -lpthread

猜你喜欢

转载自blog.csdn.net/u010420283/article/details/112528081