dynamic共享库加载框架

我正在使用传统的C库,可以通过编写用户定义的函数来进行扩展,然后重新编译源代码。 我想避免编译的要求,而是用一个函数来扩展它(见下面的伪代码):

这个function将会像这样实现:

VARIANT_TYPE CallSharedLibFunction(const char* library_name, const char* funcname, const char *params, const char* return_type){ // parse arguments and get data types and count // initiate variable of data type indicated by return_type, to hold returned variable /* this is the part I need help with */ // lptr = LoadSharedLibrary(library_name); // funcptr = GetFunctionAddress(lptr, funcname); // call function and pass it arguments retvalue = funcptr(param1, param2, param3); // wrap up returned value in the VARIANT_TYPE VARIANT_TYPE ret; setVariantValue(ret, retvalue, return_type); return ret; 

}

注意:尽pipe“Windows sounding”名称(VARIANT_TYPE,LoadSharedLibrary和GetFunctionAddress),我正在开发Linux(Ubuntu 9.10)。 理想情况下,我想图书馆加载实施是跨平台(因为我使用ANSI C代码)。 但是,如果我必须select一个平台,它将不得不成为Linux平台。

如果有人能够阐明我可以如何在任意共享库中调用函数(理想情况下是跨平台方式 – 在Linux上失败),那么我将非常感激,以便我可以实现上述function。

你可能想看看dlopen ,dlsym和类似的功能。 这些工作在POSIX(Linux,OSX,win32 + cygwin等)。

用dlopen(),你可以打开一个共享库。 你的LoadSharedLibrary可以是dlopen()的包装器。 GetFuncPtr()函数可以是dlsym()的一个包装器。 你可以做的就是围绕dl *()函数编写代码,使其强大 – 就像做一些错误检查一样。 您也可能想要在共享库中定义一个接口,即一个“导出”支持的函数的结构。 这样你可以得到一个方法列表,而不需要读取精灵文件。

在C和C ++中还有一个关于函数指针的好页面 。

以下是使用的一个简单例子:

 void* LoadSharedLibrary(const char* name) { return dlopen(name, RTLD_LOCAL | RTLD_LAZY); } void* GetFunctionAddress(void* h, const char* name) { return dlsym(h, name); } const char** GetFunctionList(void* h) { return (char**)dlsym(h, "ExportedFunctions"); } // Declare a variable to hold the function pointer we are going to retrieve. // This function returns nothing (first void) and takes no parameters (second void). // The * means we want a pointer to a function. void (*doStuff)(void); // Here we retrieve the function pointer from the dl. doStuff = GetFunctionAddress(h, "doStuff"); // And this how we call it. It is a convention to call function pointers like this. // But you can read it as 'take contents of the doStuff var and call that function'. (*doStuff)(); 

对于Linux / POSIX,您可以使用dlopen()系列函数在运行时加载共享库,查找符号地址等。

如果你想添加一个库依赖来使得可加载的代码更容易一些(而且更加便携),可以看一下glib的模块API 。

使用dlopen / dlsym以及使用-fPIC / FPIC代码进行编译