我使用以下代码片段在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
阶段没有异常,但我一直收到错误:
missing entry point undefined symbol: getMethod
在nm
上运行myLib.so
会显示getMethod
:
> nm myLib.so | grep get
0000000000024220 T getMethod
我想要,我也在该方法上使用extern "C"
。
更新
我现在怀疑虽然句柄的NULL检查不会导致异常,但句柄本身可能存在问题。
无论如何我可以对句柄执行任何进一步的检查而不仅仅是== NULL,以确认调用dlopen是否成功?
答案 0 :(得分:0)
一种可能性是存在一些失败的依赖关系,RTLD_LAZY
正在屏蔽它。请尝试使用RTLD_NOW
来查看您是否仍然可以dlopen()
共享对象。