#include <iostream>
#include <dlfcn.h>
int main() {
void* handle = dlopen("./mylibrary.so", RTLD_LAZY);
if (!handle) {
std::cerr << "Error loading shared library: " << dlerror() << std::endl;
return 1;
}
typedef void (*FunctionPtr)();
FunctionPtr my_function = (FunctionPtr)dlsym(handle, "my_function");
if (!my_function) {
std::cerr << "Error finding symbol: " << dlerror() << std::endl;
dlclose(handle);
return 1;
}
my_function(); // 调用共享库中的函数
dlclose(handle);
return 0;
}
Linux动态调用so
于 2023-09-25 09:11:39 首次发布