多线程编程-pthread 未定义的引用

多线程编程时用到函数库 pthread.h ,但是该函数库不是linux默认的函数库,所以编译c文件时,需要在目标代码文件后加上 -lpthread参数。

1.未加上 -lpthread 编译时,报错如下:

lyr@ubuntu:~/Desktop/lyr/test$ gcc multiTread.c
/tmp/cc2opFxw.o:在函数‘thread_create’中:
multiTread.c:(.text+0x153):对‘pthread_create’未定义的引用
multiTread.c:(.text+0x18b):对‘pthread_create’未定义的引用
/tmp/cc2opFxw.o:在函数‘thread_wait’中:
multiTread.c:(.text+0x1d5):对‘pthread_join’未定义的引用
multiTread.c:(.text+0x1ff):对‘pthread_join’未定义的引用
collect2: error: ld returned 1 exit status

2.正确命令:

gcc multiTread.c -lpthread

其中 multiTread.c是自己的代码文件的名字。

编译成功!!!

原文地址:https://www.cnblogs.com/lyr2015/p/7161355.html