c++ - boost asio 中的链接错误

标签 c++ linux pthreads

我在使用 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/

相关文章:

c# - 程序只能在某些电脑上运行,缺少 DLL?

C++;是否可以在子构造函数中间调用父构造函数?

c++ - 从静态构造函数代码调用时 pthread 库失败

c++ - Qmake:如何在不更改 qmake.conf 的情况下删除某个项目的编译器标志?

linux - PATH 路径前缀

linux - 创建与其他文件相关的符号链接(symbolic link)

linux - 用于 GCC/G++ 的宏来区分 Linux 和 Mac OSX?

c - 如何唤醒正在 sleep (3)的线程

c++ - 线程安全队列有问题吗?

c++ - 为什么这段代码不能使用 MS 编译器编译?