0

Let's say we have two class, Foo and Bar.

in Foo.h

#ifndef MYPROJ_FOO
#define MYPROJ_FOO
....
# endif 

in Bar.cpp, do we still need to guard the include such as

#ifndef MYPROJ_FOO
#include <Foo.h>
#endif

or simple #include is sufficient? It seems redundant to me to have include guard if we already have header guard, but I see such practice quite often, hence wondering if I'm missing something. Thanks!

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
Syenun
  • 55
  • 8
  • 2
    You don't need to have it in a .cpp file since it should not be `#include`d by any other file. – ZivS Feb 02 '17 at 21:59
  • No you don't need to do that because you don't include source files in other ones. – Raindrop7 Feb 02 '17 at 21:59
  • All major compilers will remember the include guards of files already included, so this is overkill (and error prone). – Bo Persson Feb 02 '17 at 21:59

4 Answers4

5

Functionally, it does not make any difference.

A reason why old code may be using this is as an optimisation. It means the preprocessor can avoid reading Foo.h a second time, so it can generate the result a little bit faster.

Nowadays, even if the preprocessor does read the file a second time, the performance impact of that should be small enough not to bother with it. Some preprocessors may even detect header guards automatically, remember which header guard is associated with which macro, and avoid re-reading the header file all by themselves.

4

No, you should not. Include guards are for headers, which are included by other files. Source files, on the other hand, should never be included by other files.

Alternatively you may consider using the (non standard, but generally accepted) extension in your header files

#pragma once
Jonas
  • 6,915
  • 8
  • 35
  • 53
  • 1
    `pragma` is not portable – Raindrop7 Feb 02 '17 at 22:05
  • 1
    @Raindrop7 true (as mentioned), but widely implemented by compilers. And my personal preference – Jonas Feb 02 '17 at 22:07
  • The one problem I've seen with `#pragma once` is that it is applied on a per-file basis. if you have two copies of the file in your project (common to multiple distinct modules), and they both end up included (by being deeply nested) into one source file, you get a conflict. – Mikel F Feb 02 '17 at 22:31
  • two copies of the same file? --- time to refactor! – Chad Feb 02 '17 at 22:48
  • `once` also has problems with mounts, symlinks, and other filesystem ninjitsu. My approach is use with caution and don't use with anything you plan to share. – user4581301 Feb 02 '17 at 23:28
3

in Bar.cpp, do we still need to guard the include such as

#ifndef MYPROJ_FOO
#include <Foo.h>
#endif

or simple #include is sufficient?

Simple #include is sufficient from a correctness point of view.

It seems redundant to me to have include guard if we already have header guard, but I see such practice quite often, hence wondering if I'm missing something.

The practice was recommended by John Lakos in his book Large Scale C++ Software Design.

However, a better alternative is to use the #pragma once directive, which is supported by most modern C++ compilers.

Foo.h:

#pragma once
....

Then, you can just use

#include "Foo.h"

in any .cpp file.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

or simple #include is sufficient?

Yes, it is.

It seems redundant to me to have include guard if we already have header guard,

Yes it is totally redundant.

but I see such practice quite often

I don't see such practiced, unless the codes authors have no clue what they're doing.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190