3

Possible Duplicate:
C++ Dynamic Shared Library on Linux

I am writing a shared object say libtest.so which has a class and a function. I have another program say "Program.cpp" from which i want to call the class and its function present in the libtest.so file. I am clueless as to how to proceed. Please help.

Thanks Regards Mahesh

4

1 回答 1

1

动态地,需要调用dlsym获取函数的地址,然后通过指针调用。这个语法有点棘手,因为dlsym返回 a ,并且在和指向函数的指针void*之间没有转换。void*(一些编译器确实允许它,虽然在形式上,在 C++11 之前,它需要诊断,就像 C 标准一样。)Posix 标准中推荐的解决方案是:

int (*fptr)( int );
*(void**)(&fptr) = dlsym( handle, "function_name" );

这假设指向函数的指针与指向数据的指针具有相同的大小和格式——C 或 C++ 标准不保证,但 Posix 保证。

于 2012-10-05T07:42:54.720 回答