I have two shared libraries: mylib and loglib (names are changed).
Both have destructor functions (extension of gcc).
The destructor function of mylib requires functions of loglib. This way:
mylib.c from libmy.so:
void __attribute__ ((destructor)) mylib_destructor()
{
loglib_write_log("destructor");
}
loglib.c from liblog.so:
void loglib_write_log( const char* txt )
{
fprintf( log_file, "%s\n", txt );
}
void __attribute__ ((destructor)) loglib_destructor()
{
if( log_file )
{
fclose( log_file );
log_file = NULL;
}
}
As you can see, problems occurs if loglib_destructor() is called before mylib_destructor():
fprintf will get NULL pointer parameter.
I can not change loglib.c.
How can I ensure that mylib_destructor is called before destructors of other libraries?
I don’t want to set highest priority for mylib_destructor, because users of mylib may want to use even higher priority for own destructors.
From the
gccdocs:Have you tried this?
It doesn’t have to be the highest priority for
mylib, just large enough. If it’s documented, users of your library can rely on it.