I have the following setup:
- A static library
- A dynamic library that links to (1.)
- An executable that links to (1.) and (2.)
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.
- Share symbols (default on linux)
- Add a dll export command on all symbols from the static library when linking it to the shared library.
__attribute__ ((dllexport))
- Add a dll import command when linking the static library to the executable.
__attribute__ ((dllimport))
- The code and data resides only in the shared library and is linkable from the outside
- Reduntant symbols (default on windows)
- You need to make sure that the symbols of the static library are not included in the symbol table of the shared library
__attribute__ ((visibility ("hidden")))
on gcc- When linking the executable the symbols from the static library can't be found anywhere, so they are included again.