Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C++ linkage smart enough to avoid linkage of unused libs?

Tags:

c++

linker

I'm far from fully understanding how the C++ linker works and I have a specific question about it.

Say I have the following:

Utils.h

namespace Utils {     void func1();     void func2(); } 

Utils.cpp

#include "some_huge_lib" // needed only by func2()  namespace Utils {     void func1() { /* do something */ }     void func2() { /* make use of some functions defined in some_huge_lib */ } } 

main.cpp

int main() {   Utils::func1() } 

My goal is to generate as smaller binary files as possible.

My question is, will some_huge_lib be included in the output object file?

like image 736
idanshmu Avatar asked Sep 08 '14 10:09

idanshmu


People also ask

Does linker remove unused functions?

So the linker is able to remove each individual function because it is in its own section. So enabling this for your library will allow the linker to remove unused functions from the library.

Are unused functions compiled C++?

8 Answers. Show activity on this post. It depends on the compiler. Visual C++ 9 can do that - unused static functions are removed at compilation phase (there's even a C4505 warning for that), unused functions with external linkage can be removed at link phase depending on linker settings.

What does the linker do in CPP?

The linker is a program that makes executable files. The linker resolves linkage issues, such as the use of symbols or identifiers which are defined in one translation unit and are needed from other translation units. Symbols or identifiers which are needed outside a single translation unit have external linkage.


2 Answers

Including or linking against large libraries usually won't make a difference unless you use that stuff. Linkers should perform dead code elimination and thus ensure that at build time you won't be getting large binaries with a lot of unused code (read your compiler/linker manual to find out more, this isn't enforced by the C++ standard).

Including lots of headers won't increase your binary size either (but it might substantially increase your compilation time, cfr. precompiled headers). Some exceptions stand for global objects and dynamic libraries (those can't be stripped). I also recommend to read this passage (gcc only) regarding separating code into multiple sections.

One last notice about performances: if you use a lot of position dependent code (i.e. code that can't just map to any address with relative offsets but needs some 'hotpatching' via a relocation or similar table) then there will be a startup cost.

like image 110
Marco A. Avatar answered Sep 19 '22 17:09

Marco A.


This depends a lot on what tools and switches you use in order to link and compile.

Firstly, if link some_huge_lib as a shared library, all the code and dependencies will need to be resolved on linking the shared library. So yes, it'll get pulled in somewhere.

If you link some_huge_lib as an archive, then - it depends. It is good practice for the sanity of the reader to put func1 and func2 in separate source code files, in which case in general the linker will be able to disregard the unused object files and their dependencies.

If however you have both functions in the same file, you will, on some compilers, need to tell them to produce individual sections for each function. Some compilers do this automatically, some don't do it at all. If you don't have this option, pulling in func1 will pull in all the code for func2, and all the dependencies will need to be resolved.

like image 38
Tom Tanner Avatar answered Sep 19 '22 17:09

Tom Tanner