关于dlopen用法请参考:http://baike.baidu.com/view/2907309.htm
注意:测试下面示例时要加入-ldl
示例:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
void test_dll();
int main() {
test_dll();
return EXIT_SUCCESS;
}
void test_dll() {
int first_value = 10;
int second_value = 20;
int (*add)(int a, int b);
void *handle = dlopen("/usr/local/lib/arith.so", RTLD_LAZY); //在BSD版本的Unix或Mac OS为arith.dylib, 在Linux上为arith.so, 在Windows上为arith.dll
const char *err_message = dlerror();
if (NULL == handle) {
printf("%s.\n", err_message);
return;
}
add = dlsym(handle, "add");
err_message = dlerror();
if (NULL != err_message) {
printf("%s.\n", err_message);
dlclose(handle);
return;
}
//int final_value = (*add)(first_value, second_value);
int final_value = add(first_value, second_value);
dlclose(handle);
}