且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

undefined reference to 'pthread_create'

更新时间:2022-09-20 09:38:41

Linux下编写线程函数的时候,虽然你已经把头文件

#include<pthread.h>

加入,但是会提示

thread_date.c:(.text+0x67): undefined reference to `pthread_create'
thread_date.c:(.text+0xaa): undefined reference to `pthread_join'

编译失败。

 

失败原因:

 pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。

解决方法:

 #在编译中要加 -lpthread参数
 gcc thread.c -o thread -lpthread

 本文转自cococo点点博客园博客,原文链接:http://www.cnblogs.com/coder2012/archive/2013/03/31/2991789.html,如需转载请自行联系原作者