#include #include #include "shlib.hpp" typedef void (*func)(int); void* getlib() { void* shlib = dlopen("./shlib.so", RTLD_LAZY); if (!shlib) { cerr << "dlopen: " << dlerror() << endl; exit(0); } return shlib; } func getsym(void* shlib) { void *sym = dlsym(shlib, "doit"); if (!sym) { cerr << "dlsym: " << dlerror() << endl; exit(0); } return func(sym); } int main() { void *lib = getlib(); getsym(lib)(0); dlclose(lib); lib = getlib(); getsym(lib)(1); dlclose(lib); lib = getlib(); getsym(lib)(1); getsym(lib)(2); dlclose(lib); lib = getlib(); getsym(lib)(2); getsym(lib)(1); dlclose(lib); return 0; }