Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When linking statically, does the linker include the whole library?

For example, if I static link to freeglut, does the compiler include everything from freeglut or only the parts that I use? Of course, this implies that the linker (or compiler?) do some kind of dependency analysis to figure out what it can safely exclude.

If so, is there a way to see what have been included or excluded in Visual Studio?

like image 599
subb Avatar asked Mar 10 '12 14:03

subb


People also ask

What happens when you link a static library?

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory.

How static library is 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.

Can I statically link a shared library?

You can't statically link a shared library (or dynamically link a static one). The flag -static will force the linker to use static libraries (. a) instead of shared (.

How is static linking done?

Static linking is done at link-time, which is during program development. The developer specifies the libraries on which a executable depends, and where to find them. A tool called the linker uses this information to find the proper libraries and resolve the symbols.


3 Answers

It's partly a Quality of Implementation issue, but there is a real gotcha.

Namely, by the standard the linker has to add in all compilation units that are referenced. But say that in the library, you have a compilation unit with nothing but a static variable whose initialization registers something with a something registry, e.g. message handling, factory, whatever, or perhaps its constructor and destructor output, respectively, "before main" and "after main". If nothing in that compilation unit is referenced, then the linker is within its rights to just skip it, remove it.

So, to ensure that such static variables are not optimized away, with a standard-conforming toolchain it is necessary and sufficient to reference something in that compilation unit.

Re seeing in Visual Studio what has been included, as far as I know there's no way except asking for verbose output from the linker, like, linker option /verbose:ref.

However, with that option you get really verbose output.

An alternative is to ask the linker for a map file, like, linker option /map:blah.

Also this output is very verbose, though.

like image 51
Cheers and hth. - Alf Avatar answered Oct 05 '22 22:10

Cheers and hth. - Alf


Yes, the linker will include just the translation units that your code references.

If you generate a map file for your executable then you can see exactly what it contains.

like image 23
David Heffernan Avatar answered Oct 05 '22 22:10

David Heffernan


Linker include only symbols, which are needed.

Probably, the question about inspecting *.lib files, answers the second part (dumpbin works for *.exe files too).

like image 45
Rafał Rawicki Avatar answered Oct 05 '22 22:10

Rafał Rawicki