Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#include guard before or after comment block?

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?

like image 518
V. L. Avatar asked Nov 23 '17 09:11

V. L.


1 Answers

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.

like image 68
gsamaras Avatar answered Oct 20 '22 20:10

gsamaras