0

Should I use

#ifndef _HEADER_H_
#define _HEADER_H_

etc. in every header file?

stefan
  • 10,215
  • 4
  • 49
  • 90
kutsyk
  • 185
  • 1
  • 13
  • 1
    No, you should use the same identifier. Leaving the typo aside, yes, you should guard every header. You may consider `#pragma once` as an alternative. – stefan Dec 06 '13 at 16:32
  • 1
    Names that begin with an underscore followed by a capital letter (`_HEADER_H`) and names that contain two consecutive underscores are reserved to the implementation. Don't use them. – Pete Becker Dec 06 '13 at 16:55

1 Answers1

3

Yes, it is good practice to guard every header. There are two commonly used ways to do that:

#ifndef UNIQUE_IDENTIFIER // the identifier musn't start with "_"!
#define UNIQUE_IDENTIFIER
// your code here
#endif

The second is technically not required to be supported by the compiler, but is by most modern compilers. You cannot make an error (e.g. typo, duplicate identifier) with this approach:

#pragma once
// your code here
stefan
  • 10,215
  • 4
  • 49
  • 90
  • 1
    I've also seen comments that #pragma once is slightly more efficient since it can prevent the same file from being loaded twice whereas the traditional approach still requirees the file to be loaded and the include guard to be processed. – Eric Dec 06 '13 at 16:55
  • @Eric Yes, I heard of that too. Afaik, that only ever turns out to be significant with huge headers. As I restrict myself to as small headers as possible (and think everyone should), I couldn't ever measure a significantly better performance. – stefan Dec 06 '13 at 16:57
  • @Eric I don't know of a single compiler that **doesn't** recognize include guards, and physically accesses a file only once when present, regardless of whether a classical include guard is used, or `#pragma once`. – IInspectable Dec 06 '13 at 16:58
  • @IInspectable Okay, one last try: `#pragma once` => 100% certainty that double include is forbidden. `#ifndef / #define / #endif` => additional checks if only one needed. You seriously don't see the difference? – stefan Dec 06 '13 at 17:42
  • @stefan A compiler constructs an AST. It can **easily** recognize an include guard. If it finds one, it simply pushes the file onto the same list where `#pragma once` files go. No additional checks are required. No difference. If you still see a difference, write a compiler front end, and see if that changes your mind. – IInspectable Dec 07 '13 at 01:36
  • @IInspectable Writing in boldface **won't make you right**. Agreed, compilers can easily recognize an include guard. At that point it doesn't matter if it's `ifndef` or `pragma`, same optimization. However it's still slightly more to do for parsing `ifndef`, an `identifier`, `define`, an `identifier`, nothing tampering with the `identifier` and then an `endif` and then, no other include guard. `#pragma once` expresses intent to the user _and_ the compiler in one simple statement. It's even easier to process than an include guard. That's all I've ever stated. – stefan Dec 07 '13 at 07:56