40611.c:(.text+0x1f5): undefined reference to `pthread_create'
40611.c:(.text+0x219): undefined reference to `pthread_create'
40611.c:(.text+0x22a): undefined reference to `pthread_join'
40611.c:(.text+0x23b): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
2.缘故原由
在编译时没有精确链接到 pthread 库。pthread_create 和 pthread_join 是 POSIX 线程库 (libpthread) 中的函数。
为理解决这个问题,您须要在编译命令中添加 -lpthread 选项来链接 libpthread 库。以下是精确的编译命令:
gcc 40611.c -o 40611 -lpthread
这里 -lpthread 见告 gcc 在链接阶段连接 libpthread 库。
其余,确保您的源代码中包含了 <pthread.h> 头文件,以便在编译时精确解析这些函数的声明。
如果您的程序中有其他依赖库,确保也精确链接它们。例如,如果您利用了 keyctl 函数,也须要链接 libkeyutils 库,就像之条件到的那样。