17

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?

subb
  • 1,578
  • 1
  • 15
  • 27

3 Answers3

8

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 2
    This reflects my test results. I've build a simple library with two functions in the same C++ file. When I link against this library, both functions are included, even if I only use one of them. If I split those functions into two C++ file (which results in two obj file), only the function I use in my application gets included. – subb Mar 10 '12 at 15:00
  • @PavanManjunath: in the Holy Standard it's called a *translation unit*. Effectively it's what you have after preprocessing of some implementation file (e.g. cpp file). The whole thing is described in the "phases of translation" somewhere at the start of the standard. – Cheers and hth. - Alf Mar 10 '12 at 16:00
  • It's not only a quality of implementation issue for the linker, but also for the particular library you're using. Most libraries these days are written by people who have no understanding of linkers or the purpose of translation units, and who make it so basically using any one feature of the library pulls in nearly the whole library. Try static linking a "hello, world" program that uses `printf` or even just `puts` with glibc (it'll be >500k), or worse yet, a C++ "hello world" program using iostream from libstdc++ (it'll be >900k). I suspect freeglut is awful in this regard... – R.. GitHub STOP HELPING ICE Mar 10 '12 at 21:25
7

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
4

Linker include only symbols, which are needed.

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

Community
  • 1
  • 1
Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79