当我尝试在 Fedora 中使用多线程编译 C 程序时,出现以下错误。
文件名为abc.c
abc.c:(.text+0x39): undefined reference to `pthread_create'
abc.c:(.text+0x61): undefined reference to `pthread_create'
abc.c:(.text+0x79): undefined reference to `pthread_join'
abc.c:(.text+0x8d): undefined reference to `pthread_join'
我检查了/usr/include,发现 pthread.h 存在。我还尝试将 pthread.h 复制到与 abc.c 相同的目录
如何解决这些链接错误?
最佳答案
正如 George 所指出的,您必须链接到线程库 gcc -o abc abc.c -pthread 您收到这些错误的原因是因为在链接阶段,编译器试图填充它为方法调用放置占位符的所有插槽,它知道已定义但目前不知道它们的位置,因为适当的库尚未链接然而。正如 caf 所指出的那样,在编译和链接阶段使用 -pthread 标志允许编译器在特定条件下就需要使用什么来保证线程安全做出更明智的选择。
关于c - 在 Fedora 上用线程编译 C 函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6702504/