如下
#include <dlfcn.h>
#include <iostream>
using namespace std;
class SoLoader {
public:
int loadSo(const char *SoPath) {
handler_ = dlopen(SoPath, RTLD_LAZY);
return 0;
}
void *GetFunc(const char *FuncName) {
return dlsym(handler_, FuncName);
}
~SoLoader() {
dlclose(handler_);
}
private:
void* handler_;
};
/*
gcc add.c -fPIC -shared -o libadd.so
add.c
int add(int a, int b) {
return a + b;
}
In Cmake.txt add TARGET_LINK_LIBRARIES(untitled6 dl)
*/
int main() {
SoLoader loader;
loader.loadSo("/home/huawei/CLionProjects/untitled7/libaddsub.so");
int (*addsub)(int a, int b, int c);
addsub = (int(*)(int, int, int))loader.GetFunc("addsub");//_Z6addsubiii
int res = addsub(4, 5 , 6);
std::cout << res << std::endl;
return res;
}