在ubuntu平台下调用pthread_create()函数,用gcc编译时出现Undefined reference to 'pthread_create'的问题。代码中已经将pthread.h头文件包含进去了,还是没法通过,后来通过查找资料得知pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。
知道原因后问题就很好解决了,只需在每次编译时用gcc的-l选项将pthread库链接一下就行了,例如:
gcc example.c -o example.out -lpthread
这样就可以解决pthread_create()函数没有定义的问题了。