C++ 动态加载

  动态库打开正常,但是查找函数时失败。

handle = dlopen("/home/zhq/c++/loadmodual/libhello.so",RTLD_NOW|RTLD_LOCAL);
if (handle == NULL) {
cout<<"load error."<<endl;
return ;
}
dlerror();
onload = (void (*)()) dlsym(handle,"hello_world");

运行报错:

./libhello.so: undefined symbol: hello_world
load error1.

    是由于C++编译时生成的函数名与C不一致,导致dlsym不能识别到目标函数。

解决办法:

  动态库函数声明时,加上extern "C"

#ifdef __cplusplus
extern "C"{
#endif

void hello_world();

#ifdef __cplusplus
}
#endif

posted @ 2021-09-01 11:24  zzzzhq  阅读(179)  评论(0)    收藏  举报