static variable initialisation code never gets called

后端 未结 3 884
一整个雨季
一整个雨季 2020-12-21 21:38

I\'ve got an application that\'s using a static library I made. One .cpp file in the library has a static variable declaration, whose ctor calls a function on a singleton th

3条回答
  •  北海茫月
    2020-12-21 21:57

    My memory of this is a bit hazy, but you might be getting hit with an initialization order problem. There are no guarantees in which order static variable initializers in different files get called, so if your singleton isn't initialized yet when your static variable in the library is being initialized, that might produce the effect you're seeing.

    The way I've gotten around these problems is to have some sort of an explicit init function that does this stuff and that I call at the start of main or something. You might be able to fiddle with the order in which you give the object file and library arguments to the compiler (or linker, actually) because that's also worked for me, but that solution is a bit fragile because it depends not only on using the specific linker but probably also the specific version.

提交回复
热议问题