3

The C++11 Standard states, § 3.6.2, 4:

"It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main."

The C++ Standard distinguishes static initializations, which requires only evaluation of compile time constants, from dynamic initialization. I believe the observable effects of eager static initialization are guaranteed. I'm interested in eager initialization in any case.

Given that this behavior is implementation defined and given existing implementations, is there a good portable way to force eager initialization? Is this a decision made by the compiler or linker? I think dlopen() with RTLD_NOW gives this behavior so I suspect this is possible and that it's a linker option.

To be concrete, I want to dynamically link libraries and have constructors for the globals run before main().

An example of a possible usage can be found in this question:

C++ global variable not initialized when linked through static libraries, but OK when compiled with source

The idea is to have some global registry of classes that is accessed within the main function and that classes in dynamically linked libraries can add themselves to by initializing a static variable. That static variable grabs ahold of the global registry and adds something to it. This works sometimes but is unreliable and unpredictable because it depends on undefined behavior.

Community
  • 1
  • 1
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
  • The first thing that comes to mind is to add a `void force_NAME_initialization() {}` to each translation unit, and have a function `bool force_all_initialization() {... return true;}` in the same file as `int main()` that calls each of these, as well as `static bool all_initialized=force_all_initialization();` That portably forces all globals to dynamically initialize before main begins. It's quite a bit of work though. – Mooing Duck Jul 23 '14 at 18:02
  • Given that you're probably only targeting a specific OS/compiler, I'm pretty sure there's a far easier way. – Mooing Duck Jul 23 '14 at 18:03
  • Preferably, each class could be given a chance to announce itself without needing outside knowledge of the class at compile-time (just some interface to the class). – Praxeolitic Jul 23 '14 at 18:27
  • One could.... put a `static bool NAME_is_initialized=force_NAME_initialization()` in the headers, one for each `force_NAME_initialization()` in each .cpp file, and then make sure they're all included in main.cpp. That's almost like each class announcing itself. – Mooing Duck Jul 23 '14 at 18:34
  • I think that really is the best answer staying within standard C++ and it is pretty good. Something like the GCC constructor function attribute or dlopen is needed to escape compile-time knowledge of the class. – Praxeolitic Jul 27 '14 at 04:04
  • I ran into this when compiling with intel (icpc version 15), while my project was initializing the statics just fine with g++ and visual studio compiler. I'll try TBohne's suggestion. – ikku100 Jun 19 '15 at 13:04

0 Answers0