Here you mix two different questions:
- Preprocessor Header Files
- Selective code binding using C ++ linker
Header files
They are simply copied verbatim by the preprocessor to the place where they include them. All algorithm code is copied to the .cpp file when you #include <algorithm> .
Selective binding
Most modern linkers will not contact functions that are not called in your application. That is, write the function foo and never call it - its code will not get into the executable file. So if you are #include <algorithm> and use sort only what happens:
- The preprocessor drags the entire
algorithm file into the source file - You only call
sort - The linked parses this and adds only the
sort source (and the functions that it calls, if any) to the executable. Code of other algorithms is not added.
However, C ++ templates further complicated this issue. This is a difficult problem to explain here, but in a nutshell - the templates are expanded by the compiler for all the types that you actually use. Therefore, if vector int and vector of string , the compiler will generate two copies of all the code for the vector class in your code. Since you are using it (otherwise the compiler did not generate it), the linker also places it in an executable file.
Eli bendersky
source share