C++ 对“dlsym”的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20369672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:05:24  来源:igfitidea点击:

Undefined reference to 'dlsym'

c++gcccompiler-errorsubuntu-13.10

提问by evinje

I have seen a lot of similar posts, but tried every trick in the book and am still struggling. Everything was working fine, but after installing/removing wireshark with some components/disselectors it all got messed up. I don't remember exactly which libraries/packages got uninstalled, but probably a lot more than I noticed.

我看过很多类似的帖子,但是尝试了书中的所有技巧,仍然在挣扎。一切正常,但是在安装/删除带有一些组件/选择器的wireshark之​​后,一切都搞砸了。我不记得到底卸载了哪些库/包,但可能比我注意到的要多得多。

If I create a simple main.cpp file like this one:

如果我创建一个像这样的简单 main.cpp 文件:

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

and try

并尝试

g++ main.cpp -lsqlapi -ldl

g++ main.cpp -lsqlapi -ldl

it gives me the following error messages:

它给了我以下错误消息:

/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

I have tried to put -ldl before -lsqlapi as some have suggested that the order is important. If I use gcc instead of g++ the error is:

我试图将 -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

I am able to compile and run the file if SAConnection is removed.

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

I don't think it has anything to do with SQLAPI, because I experience similar problems with libboost. I don't have a small code example, but when I compile a project that was successfully compiled last week, I get the error:

我认为它与 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

This project is using a Makefile that has been unchanged, so it has to be something on my system that is not correct. I have tried to reinstall build-essential.

该项目使用的是未更改的 Makefile,因此它必须是我系统上不正确的内容。我试图重新安装 build-essential。

Using Ubuntu 64 bit 13.10 with g++ version 4.8.1.

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

回答by evinje

I have found the solution; setting the -Wl,--no-as-needed before -ldl The new compile command is

我找到了解决办法;在 -ldl 之前设置 -Wl,--no-as-needed 新的编译命令是

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

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

Apparently it has something to do with recent versions of gcc/ld default to linking with --as-needed.

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