gcc undefined reference to `pthread_create'

写thread出现如下错误:

/data0/htdocs/c/lock.c:18: undefined reference to `pthread_create'
/data0/htdocs/c/lock.c:19: undefined reference to `pthread_create'
/data0/htdocs/c/lock.c:20: undefined reference to `pthread_join'
/data0/htdocs/c/lock.c:21: undefined reference to `pthread_join'

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

问题解决:
    在编译中要加 -lpthread参数

gcc lock.c -o thread -lpthread

你可能感兴趣的:(thread,线程,gcc)