我在使用 pthread 编译一段简单的代码时遇到以下链接错误。
The system i am working on is x86_64 (gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11) and am compiling as -
gcc -lpthread ~/temp/temp.cpp -lrt -L"/usr/lib/x86_64-redhat-linux5E/lib64/".
很明显,问题出在 pthread 库上。所以我有两个问题。
Q1。将 x64 pthread 库与 x86 代码一起使用是否可以? (在这段代码中它并不重要,但它在我的使用中很重要)(我猜不是,但我如何区分 x64 和 x86 库?)
Q2。我看到 pthread 和 lrt 都存在于/usr/lib 中,但即使我提供了该路径,它也显示 -lpthread not found。
我在这里错过了什么?感谢您的帮助。
错误:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x12): undefined reference to `__libc_csu_fini'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x19): undefined reference to `__libc_csu_init'
/tmp/cc2GQOUf.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
代码:
#include <stdio.h>
#include <pthread.h>
int main()
{
pthread_t f1_thread;
return 0;
}
最佳答案
在编译阶段,编译器可以使用gcc
或者g++
,但是g++
会自动调用gcc
命令。但是在链接阶段,链接器可以使用g++
或者gcc -lstdc++
。因为gcc
命令不能链接到c++程序使用的库自动,所以通常使用g++
来完成链接。
所以命令 g++ -g -lpthread ~/temp/temp.cpp -o temp
应该没问题。
关于c++ - boost asio 中的链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37607159/