我的主程序将加载一个名为hello.so的简单动态库。
在main函数中,使用以下代码来调用hello.so动态库中的hello()函数,并传入一个回调函数testing(在main.h文件中定义):
在main函数中,使用以下代码来调用hello.so动态库中的hello()函数,并传入一个回调函数testing(在main.h文件中定义):
void* handle = dlopen("./hello.so", RTLD_LAZY);
typedef void (*callback)();
typedef void (*hello_t)( callback);
hello_t hello = (hello_t) dlsym(handle, "hello");
hello(testing);
在动态库中的hello()函数通过以下方式调用回调函数fn:
#include
#include "main.h"
extern "C" void hello( void (*fn)() ) {
/*做一些事情,然后从主函数中调用回调函数*/ fn();
}
除了使用回调函数之外,还有其他方法可以允许从动态库中调用/使用主函数的函数/数据吗?