We are developing software which interfaces to programs written by others which have their own main() and which we cannot modify but we use the dynamic linker to intercept calls to system functions which they use.
This requires us to do things before the other program does.
We therefore must use:
/**
* Constructor routines are executed before dlopen returns
* (or before main() is started if the library is loaded at load time).
*/
void __attribute__ ((constructor)) my_initializer(void)
{ (void)new Simple("green");
}
Simple is
class Simple
{ static std::string name;
Simple(const char *);
}
----------------------------------------------
Simple::Simple(const char * name_)
{ name = name_;
}
Unfortunately this cannot work because at this stage main has not been called and so Simple::name is not initialized and therefore the memory for it is zero.
The only code which can assign to Simple::name is the compiler generated code which has not been called at this point. (This is because the assignment attempts to destroy and therefore free the string and crashes.)
In effect the code running in ((constructor)) cannot refer to static std::string objects.
The term static initializer fiasco is used but actually this is a much more simple dilemma than that. Researching this I got the impression that statics are fully initialized at the point where the first method (Simple::Simple ?) is called but this demonstrates that this is not the case.
My understanding is that there is no way to get around this. Do you know of a way to allow us to have dynamic statics in our classes?
g++ (GCC) 4.8.5