我正在使用以下代码片段在 Linux 上加载 so
库:
handle = dlopen("myLib.so", RTLD_LAZY);
if(handle == NULL) {
throw std::runtime_error("unable to load myLib.so");
}
然后我尝试访问 so
中的方法:
getter = dlsym(handle, "getMethod");
if(getter == 0) {
std::ostringstream msg;
msg << "missing entry point " << dlerror();
throw std::runtime_error(msg.str().c_str());
}
运行时,dlopen
阶段没有异常,但我不断收到错误:
缺少入口点 undefined symbol :getMethod
在 myLib.so
上运行 nm
显示 getMethod
:
> nm myLib.so | grep get
0000000000024220 T getMethod
我想要并且也在该方法上使用 extern "C"
。
更新
我现在怀疑,虽然句柄的NULL检查不会导致异常,但句柄本身可能有问题。
除了 == NULL 之外,我是否可以对句柄执行任何进一步的检查,以确认调用 dlopen 是否成功?
最佳答案
一种可能性是存在一些失败的依赖项,RTLD_LAZY
正在掩盖它。尝试使用 RTLD_NOW
来查看您是否仍然可以 dlopen()
共享对象。
关于c++ - dlopen 有效,但 dlsym 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24067498/