I figure out I can have the implementation of parts of a class in a shared lib, as far as the symbols are loaded when used.
myclass.h
---
class C {
void method();
}
main.cpp
---
#include "myclass.h"
int main() {
//dynamically load mylib.so using dlopen/dlsym/dlclose
...
C *c = new C();
c->method();
delete c;
}
mylib.so compiled separately:
====
mylib.cpp
---
#include "mylib.h"
void C::method() {
...
}
This works fine.
However once I finished using C::method(), I would like to unload it, so I can change, recompile and reload it without having to restart the main program
int main() {
//dynamically load mylib.so using dlopen/dlsym/dlclose
...
C *c = new C();
c->method();
delete c;
// close the lib, remove the handle...
....
char pause;
cin << pause; // before carrying on change the C::method, recompile it
//dynamically load *Again* mylib.so using dlopen/dlsym/dlclose
...
C *c = new C();
c->method();
delete c;
}
The problem is that it does not unload the library when doing the first dlclose, probably because an instance of my class C exists. Any way to force this?
(using g++ 4.2.3, on Linux Ubuntu 10.04)