开发者

How do C++ header files work?

When I include some function from a header file in a C++ program, does the entire header file code get copied to开发者_JAVA技巧 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.


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜