采用dlopen、dlsym、dlclose加载动态链接库
转载请标注,熬夜写的文章,挺辛苦 ...
环境
系统: 16.04.1-Ubuntu
编译器: gnu 5.4.0
python: 2.7.12
参考
dlopen、dlsym及dlclose 基本使用
// file : add.c
int add(int a, int b) { return a+b; };
// cmd: gcc -fPIC -shared -o libadd.so add.c
// 编译生成动态库文件
// file : demo.c
#include
#include // EXIT_FAILURE
#include // dlopen, dlerror, dlsym, dlclose
typedef int(* FUNC_ADD)(int, int); // 定义函数指针类型的别名
const char* dllPath = "./libadd.so";
int main()
{
void* handle = dlopen( dllPath, RTLD_LAZY );
if( !handle )
{
fprintf( stderr, "[%s](%d) dlopen get error: %s\n", __FILE__, __LINE__, dlerror() );
exit( EXIT_FAILURE );
}
do{ // for resource handle
FUNC_ADD add