views:

117

answers:

4

Hi all.

When I include some function from a header file in a C++ program, does the entire header file code get copied to the final executable or only the machine code for the specific function is generated. For example, if I call std::sort from the <algorithm> header in C++, is the machine code generated only for the sort() function or for the entire <algorithm> header file.

I think that a similar question exists somewhere on Stack Overflow, but I have tried my best to find it (I glanced over it once, but lost the link). If you can point me to that, it would be wonderful.

+3  A: 

In fact, the entire file is copied into .cpp file, and it depends on compiler/linker, if it picks up only 'needed' functions, or all of them.

In general, simplified summary:

  • debug configuration means compiling in all of non-template functions,
  • release configuration strips all unneeded functions.

Plus it depends on attributes -> function declared for export will be never stripped. On the other side, template function variants are 'generated' when used, so only the ones you explicitly use are compiled in.

EDIT: header file code isn't generated, but in most cases hand-written.

Yossarian
Thanks for your reply though.
PulpFiction
+1  A: 

If you #include a header file in your source code, it acts as if the text in that header was written in place of the #include preprocessor directive.

Generally headers contain declarations, i.e. information about what's inside a library. This way the compiler allows you to call things for which the code exists outside the current compilation unit (e.g. the .cpp file you are including the header from). When the program is linked into an executable that you can run, the linker decides what to include, usually based on what your program actually uses. Libraries may also be linked dynamically, meaning that the executable file does not actually include the library code but the library is linked at runtime.

Arkku
+3  A: 

You're mixing two distinct issues here:

  1. Header files, handled by the preprocessor
  2. Selective linking of code by the C++ linker

Header files

These are simply copied verbatim by the preprocessor into the place that includes them. All the code of algorithm is copied into the .cpp file when you #include <algorithm>.

Selective linking

Most modern linkers won't link in functions that aren't getting called in your application. I.e. write a function foo and never call it - its code won't get into the executable. So if you #include <algorithm> and only use sort here's what happens:

  • The preprocessor shoves the whole algorithm file into your source file
  • You call only sort
  • The linked analyzes this and only adds the source of sort (and functions it calls, if any) to the executable. The other algorithms' code isn't getting added

That said, C++ templates complicate the matter a bit further. It's a complex issue to explain here, but in a nutshell - templates get expanded by the compiler for all the types that you're actually using. So if have a vector of int and a vector of string, the compiler will generate two copies of the whole code for the vector class in your code. Since you are using it (otherwise the compiler wouldn't generate it), the linker also places it into the executable.

Eli Bendersky
Thanks, I get it now.
PulpFiction
A: 

It depends on the compiler. Most compilers today do flow analysis to prune out uncalled functions. http://en.wikipedia.org/wiki/Data-flow_analysis

Chad Brewbaker