今天在测试一个线程例子时,结果出现如下错误
[root@tom fzf]# gcc thread_join.c -o thread_join
/tmp/cc1HozRv.o: In function `main':
thread_join.c:(.text+0x6f): undefined reference to `pthread_create'
thread_join.c:(.text+0x91): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
问题原因:
pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a。
所以在使用pthread_create()创建线程,pthread_join()线程等待,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。
问题解决如下:在编译中要加 -lpthread参数
[root@tom fzf]# gcc thread_join.c -o thread_join -lpthread