Search code examples
linuxdlopen

How can i keep global variables in dynamic library when using dlopen to reload it


I have tried to use dlopen to load a dynamic library with the flag RTLD_NODELETE in order to keep global variables and make sure they will not be reinitiated when next time i use dlopen to reload it.

But when I want to reload the library when some functions inside the library updated, the dlopen(before reload i called the interface dlclose) seems to not work. The functions run with the same old code.

So i am confused now.

How can i keep global variables in dynamic library when using dlopen to reload it. (Update the functions but leave the variables not changed.)


Solution

  • so i am confused now.

    That's exactly what RTLD_NODELETE means: dlclose on such a library is (effectively) a no-op. Since the code effectively behaves as if dlclose has never been called, it's little wonder that subsequent dlopen doesn't reload the code either (it also becomes effectively a no-op).

    how can i keep global viriables in dynamic library when using dlopen to reload it. (update the functions but leave the virables not changed)

    You can't.