今天学习UNIX环境高级编程第11章中的线程编程,在编译第一个demo时,出现了“undefined reference to ‘pthread_create’”这种错误导致无法编译通过。
问题的原因:pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。
解决:在gcc编译的时候,命令加上 -lpthread参数即可解决。
程序11-1.c
#include "apue.h