I have a function in a static library that initalizes a static variable, with the intent that this variable is only initialized once:
static int getIndex() {
static int index = std::ios_base::xalloc();
return index;
}
Now this static library is linked by the main executable and a by shared library that the executable links:
MyApp
- MyStaticLib
- MySharedLib
- MyStaticLib
There are several calls to getIndex()
, both by MyApp and by MySharedLib. Now when I run the program, in two calls to getIndex()
, both of them through MySharedLib, the static variable index
once has the value 0 and the next time it has the value 1.
From my understanding, this should not be possible.
If I compile MyStaticLib as a shared library, the problem goes away, but I would prefer to link it statically.
I’m compiling with Clang 15 btw.