从so文件访问成员函数 [英] Access member function from so file
本文介绍了从so文件访问成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个.so和标题,其中有一个类,其中所有成员都是虚拟的
我希望在我的主要使用该类及其功能
我尝试跟随,但它给我分段错误
我无法访问.so来改变它
我尝试过:
I have a .so and header which has a class that all member of that are virtual
and I want to use the class and its functions in my main
I try following but it gives me segmentation fault
I haven't access to .so to change it
What I have tried:
CLASS_NAME * i;
void * h = dlopen("./LIBNAME.so",RTLD_NOW);
i = (CLASS_NAME *)dlsym(h, "CLASS_NAME");
i->Create(1);//member function
推荐答案
您将获得细分当i
NULL
时出错。检查是否加载库并使符号成功:
You will get a segmentation fault wheni
isNULL
. Check if loading the library and getting the symbol was successful:
CLASS_NAME * i = NULL;
void * h = dlopen("./LIBNAME.so",RTLD_NOW);
// Get error message string upon errors
char *err = dlerror();
if (!h)
{
// Report error here.
}
else
{
i = (CLASS_NAME *)dlsym(h, "CLASS_NAME");
if ((err = dlerror()) != NULL)
{
// Report error here.
}
else if (i)
{
i->Create(1);
}
}
另见 dlopen(3) - Linux手册页 [ ^ ]。
您是否有使用动态加载的具体原因?如果没有,只需将您的应用程序与库链接。
See also dlopen(3) - Linux man page[^].
Is there any specific reason that you are using dynamic loading? If not, just link your application with the library.
这篇关于从so文件访问成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文