How do C ++ header files work?

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

I think a similar question exists somewhere in Stack Overflow, but I struggled to find it (I looked at it once, but lost the link). If you can point me to this, it would be great.

+7
c ++ header-files
source share
4 answers

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.

+10
source share

In fact, the entire file is copied to a .cpp file, and it depends on the compiler / linker if it takes only the "necessary" functions or all of them.

In general, a simplified summary:

  • Debugging configuration means compiling in all non-template functions,
  • release configures all unnecessary features.

Plus it depends on the attributes -> a function declared for export will never be deleted. On the other hand, options for function templates are "generated" when used, so only those that you explicitly use are compiled.

EDIT: The header file code is not generated, but in most cases written by hand.

+3
source share

If you #include header file in the source code, it acts as if the text in this header was written instead of the #include preprocessor directive.

Typically, headers contain ads, i.e. information about what's inside the library. Thus, the compiler allows you to name things for which the code exists outside the current compilation unit (for example, the .cpp file from which you include the header). When a program is associated with an executable file that you can run, the linker decides what to include, usually based on what your program uses. Libraries can also be linked dynamically, which means that the executable does not actually contain library code, but the library is linked at runtime.

+1
source share

It depends on the compiler. Most compilers today analyze the thread to trim unclaimed functions. http://en.wikipedia.org/wiki/Data-flow_analysis

0
source share

All Articles