参考链接:https://blog.csdn.net/chenjinlong126/article/details/78990350
一.制作so文件:libadd_c.so或libadd_cpp.so
1、add.c:
int add(int a, intb)
{return a +b;
}
编译:
gcc -shared -fpic -lm -ldl -o libadd_c.so add.c
2、add.cpp:
extern "C"{int add(int a, intb)
{return a +b;
}
}
编译:
gcc -shared -fpic -lm -ldl -o libadd_cpp.so add.cpp
2.编写测试函数
test.cpp:
#include #include#include#include
using namespacestd;intmain()
{int a = 0;void *handle = dlopen("./libadd_c.so", RTLD_LAZY);if(!handle)
{
printf("open lib error\n");
cout<
}
typedefint (*add_t)(int a, intb);
add_t add= (add_t) dlsym(handle, "add");if(!add)
{
cout<
dlclose(ha