今天在测试一个线程例子时,结果出现如下错误
[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
本文介绍了一个关于pthread库未正确链接导致的编译错误案例。通过分析问题根源,明确了在使用pthread_create()等函数时需要正确链接pthread库。文章最后给出了具体的解决方案,即在编译命令中加入-lpthread参数。

被折叠的 条评论
为什么被折叠?



