我可以加载函数从动态库与链接此dl,但我不能加载它使用'dlsym'在代码中没有链接这个dl [英] I can load functions from dynamic library with linking this dl ,but I can not load it using 'dlsym' in the code without linking this dl

查看:262
本文介绍了我可以加载函数从动态库与链接此dl,但我不能加载它使用'dlsym'在代码中没有链接这个dl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在fedora 116上使用gcc / g ++,我的想法是:

I am using gcc/g++ on fedora 116, and my idea is:

c program -> load c++ dynamic library A -> load c++ dynamic library B

c ++动态库B是第三方提供的,我不能修改它。

The c++ dynamic library B is third-party provided and I can not modify it.

当使用链接c ++动态库B补充c ++动态库A时,A可以在B中找到符号。但是当我在A代码中加载B函数'dlsym',A告诉我

When complinng c++ dynamic library A with linking c++ dynamic library B, A can find symbols in B. But when I load B functions in A code (not linking) using 'dlsym', A tells me

/path/to/B.so: undefined symbol: some_func

============================= ==

=============================

使用nm -DC

0000000000014a80 T BinarySearch(int, int*, int)
0000000000007210 T CheckLicense()
0000000000009370 T GetEnd(stCha*, int&, int)
000000000000a970 T IC_Exit()
000000000000a740 T IC_Init(char const*)

错误报告:

/path/to/some.so undefined symbol: IC_Init

库中的代码:

IC_API bool (* IC_Init)(const char *);
IC_Init = (IC_API bool (*)(const char *)) dlsym(dl_ic, "IC_Init");
if(IC_Init) {
    printf("function loaded");
}

,可以使用dlopen加载库B:

in library A, it can load library B using dlopen:

void *dl_ic = dlopen(ic_lib_path, RTLD_LAZY);


推荐答案

C ++标识符通常被篡改以合并关于它们的命名空间和参数的信息(历史上帮助链接器区分重载函数)。您可能想要使用函数 externC来防止损坏,或者找到它的修改名称使用 dlsym (例如在Linx上使用对象上的 nm ,或 gcc -S -o / dev / tty ... | grep some_func on the source)。

Have you considered name mangling? C++ identifiers are typically "mangled" to incorporate information on their namespace and arguments (which historically helped linkers differentiate overloaded functions). You may want to make the function extern "C" to prevent mangling, or find its mangled name to use with dlsym (e.g. on Linx use nm on an object, or gcc -S -o /dev/tty ... | grep some_func on the source).

这篇关于我可以加载函数从动态库与链接此dl,但我不能加载它使用'dlsym'在代码中没有链接这个dl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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