Is linking two DLLs to the same static library and then linking both DLLs together a violation of the ODR?
Asked Answered
E

0

6

I have come across a situation in our codebase where two DLLs that link to each other, both link statically to the same static library. This results in both DLLs pulling in a separate copy of the static library when they are linked.

enter image description here

In this particular situation, the static library contains a class that is intended to be a singleton... But since both DLLs pull in their own copy, when DLL1 attempts to access the singleton it gets a different instance than DLL2! This is causing lots of issues with program state and initialization because both libraries see a different state of the program.

I understand what is happening is BAD and is a bug in the program. But is it actually in the realm of "undefined behavior"? Does this violate the One Definition Rule?

What about the situation where the static library does not contain a singleton. Is it perfectly okay then, or is that still a problem? (If it violates the ODR, I assume it is still a problem.)

Eroticism answered 8/11, 2019 at 15:25 Comment(7)
Put the singleton in a different dllKopeck
@Kopeck That's what I'm doing, but there's things other than just the singleton in the static library. Is it fine for those things to stay in the static library?Eroticism
Even though I prefer making your static library into a DLL, I note that boost 1.68.0 headers-only form can be built into more than one DLL and it will work as expected re globals. So that suggests cleverly making the static library into a headers-only kind of a deal could also be a way out of this flavor of DLL hell.Canaster
@Canaster I am actually curious how this works with header only libraries... I would think you would run into the same problem.Eroticism
I believe you are safe as long as you link to the same exact static library implementations. I can't answer what the standard says about this. Your singleton problem is likely because each dll created its own instance of a static variable.Kopeck
I think this is related: https://mcmap.net/q/134805/-what-happens-to-global-and-static-variables-in-a-shared-library-when-it-is-dynamically-linkedKopeck
Thanks @drescherjm, the answer to that question clarifies quite a bit about how things actually work.Eroticism

© 2022 - 2024 — McMap. All rights reserved.