接收“未定义符号"使用 dlopen 加载库时出错 [英] Receive "undefined symbol" error when loading library with dlopen

查看:61
本文介绍了接收“未定义符号"使用 dlopen 加载库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些使用动态共享库作为插件的代码.

I'm writing some code that uses dynamic shared libraries as plugins.

我用于构建共享库的命令行如下所示:

My command line for building the shared libraries looks like:

cc -shared -fPIC -o module.so -g -Wall module.c

在模块中,我可以调用已加载到主可执行文件中的任何其他共享库中的函数.

Within the module, I can call functions that are in any other shared library that has been loaded within the main executable.

但是,我无法访问(导出)可执行文件本身中的函数(我收到 undefined symbol 错误).

However I cannot access (exported) functions that are in the executable itself (I get undefined symbol errors).

我对 dlopen 的调用如下所示:

My call to dlopen looks like this:

void *handle = dlopen(plugin, RTLD_NOW);

谁能建议我的模块如何回调我的可执行文件,而不必将所有可执行文件的实用程序函数放入另一个共享库中?

Can anyone please advise how my module can call back to my executable, without having to put all of the executable's utility functions into yet another shared library?

推荐答案

我自己找到了答案.

我必须将 --export-dynamic 标志添加到主可执行文件的链接选项中.

I had to add the --export-dynamic flags to the link options for the main executable.

创建动态链接时可执行文件,将所有符号添加到动态符号表.动态的符号表是一组符号从动态对象中可见在运行时.

When creating a dynamically linked executable, add all symbols to the dynamic symbol table. The dynamic symbol table is the set of symbols which are visible from dynamic objects at run time.

如果您不使用此选项,则动态符号表通常会只包含那些符号被某个动态对象引用链接中提到.

If you do not use this option, the dynamic symbol table will normally contain only those symbols which are referenced by some dynamic object mentioned in the link.

如果您使用dlopen"加载动态需要回溯的对象程序定义的符号,而不是其他一些动态对象,那么你可能需要使用链接程序时此选项自己.

If you use "dlopen" to load a dynamic object which needs to refer back to the symbols defined by the program, rather than some other dynamic object, then you will probably need to use this option when linking the program itself.

这篇关于接收“未定义符号"使用 dlopen 加载库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆