7

I have somewhere read (sorry can't find the link anymore) that on the first line of a header should always be the #include guard, because compilers can see it without opening the header file. So if a header file has already been included, it won't open the file just to close it again and this speeds up the building process.
But I always have a comment block at the start of every file. So my question is, should the #include guard be written before the comment block or after?

Is this style better:

///////////////////////
// Name:        code.h
// Author:      Me
// Date:        dd.mm.yyyy
// Description: This code executes a specific task
///////////////////////

#ifndef CODE_H_
#define CODE_H_

...

#endif

Or ist this style better:

#ifndef CODE_H_
#define CODE_H_

///////////////////////
// Name:        code.h
// Author:      Me
// Date:        dd.mm.yyyy
// Description: This code executes a specific task
///////////////////////

...

#endif

Or doesn't it matter at all?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Vuks
  • 801
  • 1
  • 7
  • 24
  • If you really want to safe some nanoseconds, you MUST WRITE IT AT THE FIRST POSSIBLE PLACE! ;) Such things are really important for the life of all software guys. If that is one of the most important problems you have, you have a very good position! BTW: The compiler must open the file anyway... The include gard is not represented elsewhere. – Klaus Nov 23 '17 at 09:51
  • @Klaus I know that this isn't really a big thing, but I just got curious. :) – Vuks Nov 23 '17 at 09:55
  • Before. The fewer lines the preprocessor has to process, the better. I used to have a compiler (Watcom) that would print out direct and included line counts, and it was always in the ratio of at least 1:100. – user207421 Nov 23 '17 at 09:56
  • 3
    It's a pity you can't find a link, because otherwise I would tell you to ignore everything that source says from now on. That is, if it indeed says what you claim it says. It is likely that you misremember. Compilers cannot see include guards without opening the file. What they can do, and indeed do, is remember that a certain file has include guards so that they can avoid opening it *again*. It doesn't matter if the guard is the first thing in the file or follows some comments. – n. m. could be an AI Nov 23 '17 at 10:15

1 Answers1

5

From my experience and others'

is that it's NOT the parsing and reading of files that take the time, but the various optimisation and code-generation passes

So opening a file to parse the inclucde guard should be negligible for a large project's build time.

I mean it couldn't be the bottleneck of the procedure!

So choose either style you prefer, it's really opinion based.


Another interesting comment found here:

One upon a time, there might have been one or two compilers stupid enough to open the file each time to check the include guard. No compiler produced in this millennium would do that, as it can just keep a table of files and include guards and consult that before opening the file.

Read more in File-wide include-guards.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • The question is not to use include guards or not, it asks for before or after a comment block. So optimisation and code-generation passes are not relevant here. – Klaus Nov 23 '17 at 09:54
  • 1
    @Klaus yes but my point is to show that parsing and reading the file is really done fast enough to not worry the OP. How would you propose to edit my question? Also my answer doesn't intend to answer the use or no use of guards. It answers the OP's question I believe. =) – gsamaras Nov 23 '17 at 09:55