Christopher Pisz
Christopher Pisz

Reputation: 4010

Multiple instances of static or global through linking

I have run into a situation where some dependency code is a static library.

Let's call it static library "A". I have made a dll "B" and it links to "A". And an executable "C" links to both.

I think this diagram is correct:

            A  static lib
           / \
          |   B   dll
           \   \
            \---C  executable

"A" contains a logger class that uses global variables. It's one of those your company hands you, you look at it, and say to yourself, "oh crap what a mess."

It appears that those global variables in "A" have different addresses depending on whether I am stepping through code in the executable module or stepping through the code in the dll module.

This, of course, makes the logger behave in an unexpected manner.

I assume the static library was copied once into the dll and once into the executable and there are therefore actually 2 copies of all statics and globals. Is that correct?

The second part of the question is, will making this logger a Meyer's singleton solve the problem or will a static in method scope also have different addresses depending which module I call from?

Or, is the proper thing to do, making all 50 some projects link dynamically if one links dynamically?

Upvotes: 0

Views: 323

Answers (1)

Donnie
Donnie

Reputation: 46913

Every executable that logs must currently have linked in the static library. Note that a DLL is considered to be an executable here.

Therefore, both your DLL and your EXE have their own copy of said static library, with their own variables and so forth. Your only real recourse is to move logging from a static library to a DLL, or make it so that multiple instances logging at once works in a useful and expected manner.

Upvotes: 3

Related Questions