Is there a performance hit when including unused header files in C / C ++?

I have a project in which every C / C ++ file uses a bunch of header files. But about 70-80% of the header files that each C / C ++ file uses are the same. Therefore, to make my code more readable, I plan to include all the headers that I need in the project in one header file, say common_headers.h and include it in all my C / C ++ files, for example:

 #include "common_headers.h" 

Now this will include all the necessary headers, but also some additional headers that will not be used by a separate file. I want to know if this will be done, will it succeed at runtime?

I am well versed in a few milliseconds to compile the code, but want to know if this will affect performance at runtime?

Description of the headers used:

  • Most of them are standard C / C ++ headers.
  • Custom headers have built-in template features.
  • The user does not have static functions defined headers.

This is my compiler: g ++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)

+6
source share
4 answers

GENERALIZATION:

If something is included, it should be analyzed, even if it will never be compiled and linked, so the compilation time will probably increase - Do not include unused headers .

RUNTIME:

It has already been mentioned by @DonReba that unused headers may contain some pragma directives that can modify the resulting executable, but this is usually not the case.

Most unused functions and declarations will be optimized, except for some specific cases - Will unused functions be optimized? . The exe result may become a little big, but these functions and variables will not be used, so the overall impact will be minimal. - - However, do not include unused headers .

SUMMARY:

If you can change the source code to not include anything superfluous, change it.

Personnaly I prefer to have standalone modules (headers) that include everything they need - nothing more, nothing less. Such modules can be added and removed without delay and the possibility that some unnecessary dependency remains. They are still not a panacea, but in combination with attentiveness and some code analysis, they will keep your program free from deadweight files.

EDIT:

Precompiled headers:

Precompiled headers are used to reduce compilation time for commonly used but rarely modified headers (system headers, large project headers), so if these unused headers are included in the precompiled header, then the effect of compile time during subsequent compilations will be minimized. However, all run-time problems, regardless of how small they are, remain the same as for a simple header.

+9
source

Short answer to the asked question: No

Long answer:

Other headers mean that the likelihood of a problem can appear as a performance problem, but it really is not a problem.

Many people think that this is a bad style, as you plan, but there are those who consider it acceptable.

One of the main reasons to avoid this style is that it will make it easier to get circular dependencies.

I would discourage it due to a compilation problem when I don't have precompiled headers.

+2
source

Depends on the compiler. Today, most compilers are smart and use precompiled headers to improve performance. I use the GCC , which supports a precompiled header, and AFAIK does not affect performance.

+1
source

Perhaps including an extra header will create different runtime code due to overriding the preprocessor directive. But this will not be a normal situation.

Visual C ++, GCC, and Clang support precompiled headers to improve compilation time for cases like yours.

0
source

Source: https://habr.com/ru/post/973010/


All Articles