3 ms·
Dunno which article it was, but if you're saying that header guards don't completely alleviate the compile times, you're correct (In order to hit the guard or #
by lelanthran 3y ago
Dunno which article it was, but if you're saying that header guards don't completely alleviate the compile times, you're correct (In order to hit the guard or #pragma, the file still has to be opened and read).
The way to do it is to never have any `#include` directives in headers. Put them all in the implementation source files.
You have a function in a `foo.h` which returns a `bar_t` type, that is defined in `bar.h`? Then each source (.cpp or .c) file that includes `foo.h` must first include `bar.h`.
While this does not completely alleviate the problem of reading the same header 10x in a project with 10 source files, it does mean that at least you don't read the same same 50x in a project with 10 source files.
Each header is read once, and only once, for each translation unit. When using header guards or #pragmas, a single header file will be read multiple times because it can (and usually will) include other headers.
- andrewmcwatters 3y agoThanks for mentioning this, I believe it was a part of the strategy they used for this article I’m thinking about.