I have the following setup:
The code from the from the static library is now duplicated and present in the dynamic library and the executable.
Questions:
Is the Data (global variables, static class members) also duplicated and does the executable and the dll see the same data?
Is there a difference between Linux and Windows?
How would you solve this?
Edit:
Thanks for the answers, I can now explain what happened in my case exactly.
The static library had no export/import flags. The dynamic library had export on its own symbols.
Windows:
The dynamic library had a copy of the text+data segement of the static library. The executeable couldn't know, that the dynamic library had linked the static library, because non of the static-library symbols are visible from the outside.
Linux:
The dynamic library had a copy of the text data segment of the static library and included all symbols (text and data) from the static library in its own symbol table. -> The executable sees, that the dynamic library has already defined all symbols of the static library and does not redefine them.
This is bad because you usually want the same behavior on linux and on windows.
__attribute__ ((dllexport))
__attribute__ ((dllimport))
__attribute__ ((visibility ("hidden")))
on gccStatic libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.
Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are, . a files in Linux and . lib files in Windows.
Another benefit of using static libraries is execution speed at run-time. Because the it's object code (binary) is already included in the executable file, multiple calls to functions can be handled much more quickly than a dynamic library's code, which needs to be called from files outside of the executable.
What are the differences between static and dynamic libraries? Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.
As far as I know, it depends on the operating system (because C++ language doesn't say much about how libraries should work).
On windows, you'll get twice the code and data, and worse of that all twice the global variables declared in that library (!)
This issue shows up when linking statically the standard library in a program and a library using that, when you get two default allocators and if you call new
on a library and delete
on the other, the object will leak on the new
side and the heap is likely to become corrupted on the delete
side.
I do not know the details about other operating systems, but I expect similar issues may arise
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With