c++ - 未定义对 'dlsym' 和 'dlopen' 的引用

标签 c++ linux arm shared-libraries undefined-reference

我正在使用 arm-linux-gnueabi-g++ 版本 4.7.3 进行编译。

我在以下位置安装了 arm-linux-gnueabi 库:

/usr/arm-linux-gnueabi/lib, it contains libdl.a, libdl.so, libdl.so.2, and libdl-2.19.so.

libdl.so 链接到 libdl.so.2,后者链接到 libdl-2.19.so。

我正在尝试链接到 dl 库(请参阅下面的命令字符串),但我总是遇到 undefined reference 错误。

arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux  -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status

如果我使用以下命令使用 g++ 4.8.2 进行编译,那么我的程序可以正常编译、链接和执行。

g++ -I. -I../ -I../Comms/Linux  -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl

很明显找不到libdl.so库;我认为通过使用 -L 标志将路径添加到适当库的位置可以解决问题,但事实并非如此。

ARM 编译器命令缺少什么?

最佳答案

嗯,我找到了答案,我需要 -Wl,--no-as-needed 标志在 -ldl 之前。在我问这个问题之前,我遇到过这个标志,但显然打错了,因为它对我不起作用。

我不明白为什么需要这个标志,但代码现在确实完成了链接。

SO 用户 here说它与链接到 --as-needed 的 gcc 的最新版本(截至用户发布的 2013 年)有关。

关于c++ - 未定义对 'dlsym' 和 'dlopen' 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27607130/

相关文章:

C++ boost 日期格式

c++ - 分配具有可变长度数组成员的结构

c++ - 静态字段的析构函数。单例实现

Linux 上的 Android Studio 2.3(及更早版本)无法加载项目 - Gradle 同步失败 : Index: 0, 大小:0

c++ - 为什么我的程序在 _fini 中花费了 85% 的 CPU 周期?

c - Linux C : Accessing shared memory fails with `Invalid Argument` even though it was just created

c - 使用 open 读取仅使用一个值的文本文件并尝试锁定该文件

c - 编译器是否在作用域中使用堆栈变量的相对地址?

arm - 错误: L6236E: No section matches selector - no section to be FIRST/LAST

assembly - ARM架构中的GT和HI指令有什么区别?