C++: link library twice, are global constructors run twice?

Viewed 342

If I have a library foo that has some static initialization code in it, and a linking situation as follows:

 executable -> libShared.so (dynamic linking)
 executable -> libFoo.a (static linking)
 libShared.so -> libFoo.a (static linking)

Does this result in the static initialization code being run twice - and if it has side-effects, possibly doing two different visible things?

libShared.so has done the right thing by only exporting its own symbols, and not exporting any of libFoo.a. But there are two copies of libFoo.a in the process, effectively namespaced away from one another. Is my understanding right - there'll be a duplicate initialization step?

(Naturally the right approach is to have libFoo.a not do anything like create visible side-effects in its static initializers, but let's assume that ship has sailed...).

2 Answers
Related