1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include <dlfcn.h>
#include <iostream>
typedef void (*HelloFunc)();
int main() { void* handle = dlopen("./libhello.so", RTLD_LAZY); if (!handle) { std::cerr << "dlopen failed: " << dlerror() << std::endl; return 1; }
dlerror();
HelloFunc hello = (HelloFunc)dlsym(handle, "hello"); const char* dlsym_error = dlerror(); if (dlsym_error) { std::cerr << "dlsym failed: " << dlsym_error << std::endl; dlclose(handle); return 1; }
std::cout << "Address of hello function: " << reinterpret_cast<void*>(hello) << std::endl;
hello();
dlclose(handle); return 0; }
|