Are GCC shared-library constructors and destructors, defined with __attribute__((constructor))
and __attribute__((destructor))
, guaranteed to run exactly once? The documentation seems to imply that they will be run at least once, but doesn't mention anything about more than once.
In other words, if I do an operation in the constructor that must be done only once, do I need to protect it like so:
static gboolean constructor_has_run = FALSE;
if(!constructor_has_run) {
do_operation();
constructor_has_run = TRUE;
}
An observation that might be useful if someone wants to use such functions in headers: if a function is defined like
__attribute__((constructor)) inline void fn()
{ ... }
in N translation units, it will be called N times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With