我正在用C编写一个插件应用程序,我使用dlopen/dlsym动态加载某些函数的“实现”。例如,我有以下指向函数的指针
struct cti_t* (*create)() = 0;我使用以下代码加载实现:
plugin_handle = dlopen ("xxx.so", RTLD_NOW);
//error checking
create = dlsym(plugin_handle, "cti_create");
//error checking
//call create for the specific implenetation
struct cti_t *dev = create();“插件”定义cti_create的方式如下
struct cti_t* cti_create(int i) {
printf("Creating device lcl");
//do somenthing with i
return &lcl_cti;
}因此,它定义了一个整数参数,但所有操作都没有错误。问题是:当符号加载dlsym时,是否可以进行参数类型验证?如何确保加载的符号具有我期望的签名?
发布于 2014-03-10 13:49:24
如果函数是C函数,则在使用dlsym加载时,不能进行任何参数类型验证--图像中没有任何参数定义参数(或返回类型)。如果您正在使用C++ (并且没有声明符号具有extern "C"链接),那么类型检查将嵌入到实际的符号名中。话虽如此,在调用dlsym()时,您必须输入损坏的C++名称,而不是"cti_create“。
https://stackoverflow.com/questions/22301812
复制相似问题