13
Yes. When a library is opened, all static construction takes place... so, if you use C++, you can do:
// mylibrary.cpp
namespace
{
class dynamic_library_load_unload_handler
{
public:
dynamic_library_load_unload_handler(){
// Code to execute when the library is loaded
}
~dynamic_library_load_unload_handler(){
// Code to execute when the library is unloaded
}
} dynamic_library_load_unload_handler_hook;
}
Unlike the __attribute__ ((constructor)) solutions given, this will be portable. Note, though, that if you have multiple objects like this, there is no guarantee with respect to the construction/destruction order.