Search code examples
c++linuxg++static-linkingdynamic-linking

When I compile a C++ program on Linux using the STL, how is the STL library linked?


When I compile a program which uses the STL using g++, how does the library get linked to my program? Dynamically or statically?

Statically sounds odd to me as that would mean every single C++ program which uses the STL would have to include it internally. On the other hand, dynamic linking sounds also odd to me as with all the OOP stuff I do not see how a library can be linked dynamically and also support different kinds of objects...

So what exactly is happening here?


Solution

  • The answer is in your question: STL stands for "Standard Template Library". As templates are in header files and are only instantiated when they are needed (e.g. used), you can include every single STL header (if you wanted), and if you did not use any of them, your binary would be no larger.

    The STL is not a .lib or .a file that must be linked. It is a collection of header files.