对'dlsym'的未定义引用

23

我看过很多类似的帖子,但尝试了所有诀窍,仍然苦苦挣扎。一切都运转良好,但在安装/卸载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

我尝试按照一些人建议的顺序,在-lsqlapi之前加上-ldl。 如果我使用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。

1个回答

61

我找到了解决方案; 在-ldl前设置-Wl,--no-as-needed。新的编译命令如下:

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接