According to Wikipedia this is what the C preprocessor does:
"The preprocessor replaces the line
#include <stdio.h>
with the text of the file 'stdio.h
', which declares theprintf()
function among other things."
So if this is true, a program which includes more header files would take more time to compile?
Only source files are passed to the compiler (to preprocess and compile it). Header files aren't passed to the compiler. Instead, they are included from source files.
A compiler takes the program code (source code) and converts the source code to a machine language module (called an object file). Another specialized program, called a linker, combines this object file with other previously compiled object files (in particular run-time modules) to create an executable file.
Yes it is possible to write a simple program without header files, but why would you do that ? Header files are useful to share definitions, constants, functions prototypes, etc between multiple files or modules.
GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets.
So if this is true, a program which includes more header files would take more time to compile?
Of course. Strictly speaking, the more code the compiler needs to look at, the more time it needs to process it. For some really big projects, the amount of time needed to look at all the files easily becomes a concern. This is especially true for extraordinarily large and/or complex template code, which for practical reasons must reside in header files. The organization of the header files themselves also have an impact on compilation time.
However, it's not as simple as you think it is. It is highly dependent on the quality of implementation (QOI) of the compiler, and modern compilers nowadays are actually quite good at handling header files in most cases.
For example, GCC specifically recognizes include guards to reduce processing time. And compilers nowadays are getting much better at handling complex template code e.g. most of the standard library. On VC++ compilers, including windows.h
(which has function prototypes for almost the entire Windows API) does not appreciably increase compile times in my experience. And if all else fails, many if not all compilers have a "precompiled headers" feature you can use.
Basically, don't worry about it until it becomes a problem. If having more header files helps to better organize your code, then by all means don't hesitate to use them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With