dlopen有效,但dlsym没有

时间:2014-06-05 18:25:54

标签: c++ c linux

我使用以下代码片段在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是否成功?

1 个答案:

答案 0 :(得分:0)

一种可能性是存在一些失败的依赖关系,RTLD_LAZY正在屏蔽它。请尝试使用RTLD_NOW来查看您是否仍然可以dlopen()共享对象。