对'dlsym'的未定义引用

Undefined reference to 'dlsym'

本文关键字:引用 未定义 dlsym      更新时间:2023-10-16

我看过很多类似的帖子,但我尝试了书中的每一个技巧,仍然在挣扎。一切都很好,但在安装/拆卸了wireshark和一些组件/拆卸器后,一切都一团糟。我不记得具体卸载了哪些库/包,但可能比我注意到的要多得多。

如果我创建一个简单的main.cpp文件,如下所示:

#include <SQLAPI.h>
int main()
{
  SAConnection con;
  return 0;
}

并尝试

g++main.cpp-lsqlapi-ldl

它会给我以下错误信息:

/usr/local/lib/libsqlapi.so: undefined reference to `dlsym'
/usr/local/lib/libsqlapi.so: undefined reference to `dlerror'
/usr/local/lib/libsqlapi.so: undefined reference to `dlopen'
/usr/local/lib/libsqlapi.so: undefined reference to `dlclose'
collect2: error: ld returned 1 exit status

我试着把-ldl放在-lsqlapi之前,因为有些人认为顺序很重要。如果我使用gcc而不是g++,错误是:

/usr/bin/ld: /tmp/ccwBI4tj.o: undefined reference to symbol '__gxx_personality_v0@@CXXABI_1.3'
/usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

如果删除了SAConnection,我就可以编译并运行该文件。

我不认为这与SQLAPI有任何关系,因为我在使用libboost时也遇到过类似的问题。我没有一个小代码示例,但当我编译上周成功编译的项目时,我得到了错误:

/usr/bin/ld: debug/components/helloworld/HelloWorld.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/usr/lib/x86_64-linux-gnu/libboost_system.so.1.53.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

这个项目使用的是一个未更改的Makefile,所以它在我的系统中一定是不正确的。我已尝试重新安装build essential。

使用Ubuntu 64位13.10与g++版本4.8.1。

我找到了解决方案;将CCD_ 1设置在CCD_。新的编译命令是:

gcc main.cpp -lsqlapi -lstdc++ -Wl,--no-as-needed -ldl

显然,这与最新版本的gcc/ld默认链接--as-needed有关。