Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static library linked two times

Tags:

c++

I have the following setup:

  1. A static library
  2. A dynamic library that links to (1.)
  3. 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.

  1. 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
  1. 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.
like image 770
dari Avatar asked Jul 03 '15 14:07

dari


People also ask

How are static libraries linked?

Static 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.

Is .LIB a static library?

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.

Are static libraries faster?

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 is the difference between static and dynamically linked libraries?

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.


1 Answers

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

like image 199
pqnet Avatar answered Oct 22 '22 17:10

pqnet