6

Is it good practice to use your choice of either/both include guards and #pragma once in every header file, or just those with something such as a class declaration?

I am tempted to put it in every header, but I'm afraid it would be unneeded and only add to the compile time. What is good practice or common to do?

Let me clarify: I understand the difference between the two. I am asking whether by experience programmers use it in every file or just those that require it.

Matthew D. Scholefield
  • 2,977
  • 3
  • 31
  • 42
  • 4
    IMHO You should put include guards in every header file. There is no point in the compiler re-compiling stuff that it already included and for maintenance reasons, even of you have a header that doesn't create an error if it is included twice, someone later may add something that does. – Galik Dec 25 '14 at 19:11
  • OK, that brings up a point I didn't think about: The amount of extra compile time it adds would actually speed it up since it would not have to re-copy the header. – Matthew D. Scholefield Dec 25 '14 at 19:13
  • See this question as well:http://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards?rq=1 – wingerse Dec 25 '14 at 19:23
  • As far as I can tell most compilers are smart enough to detect include guards and omit looking at protected files they have read. The inventers of `#pragma once` either were not clever enough or wanted to assure vendor lock-in. Apply Occam's Razor to decide which one it was... – Dietmar Kühl Dec 25 '14 at 19:25

3 Answers3

8

Summarizing the comment by Galik and what I realized:

Include guards should be put in every header file in the case that something in the future conflicts. Furthermore, the small time it takes the compiler to process the include guards will make the compilation faster since the extra header does not need to be processed.

Matthew D. Scholefield
  • 2,977
  • 3
  • 31
  • 42
6

#pragma once is compiler specific, while normal include guard definitions, should work with any c++ preprocessor.

Thus for sake of portability, always stick to "ye good old" include guards idiom.

The use of include guards is essential to prevent multiple symbol declaration errors in your code. You shouldn't bother about assumptions, or implications, how this is handled by the c++ preprocessor (modern ones are pretty optimized to do this efficiently).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Please look at the clarification. – Matthew D. Scholefield Dec 25 '14 at 19:10
  • 5
    Also the portability claim is bogus, since `#pragma once` is supported by every single relevant compiler out there. – Griwes Dec 25 '14 at 19:13
  • @RandomPerson323 Galik's comment on your question well serves you. Put unique include guards into every header file, dont use `#pragma once`! period. – πάντα ῥεῖ Dec 25 '14 at 19:13
  • @Griwes You're working a lot with embedded, CPU specific compilers do you? – πάντα ῥεῖ Dec 25 '14 at 19:14
  • @πάνταῥεῖ Yes, Galik's comment answers the question. And it all depends on the project as whether to use `#pragma` or include guards. – Matthew D. Scholefield Dec 25 '14 at 19:16
  • @πάνταῥεῖ (btw having a nick that is not inputtable by the majority of keyboards is not nice): no, but I must say I doubt the relevance; it's 2014, we have technology that lets us not care about that. – Griwes Dec 25 '14 at 19:16
  • @Griwes Well, let's close this question as being _"opinion based"_ then. Nothing more to say ... (and BTW you have at least `@` at your keyboard I'd suppose?) – πάντα ῥεῖ Dec 25 '14 at 19:18
  • The question has nothing to do about which to use. it is about *when* to use it. – Matthew D. Scholefield Dec 25 '14 at 19:20
  • 2
    @RandomPerson323 _"it is about when = to use it"_ Well, then the answer is: ***Everytime***. – πάντα ῥεῖ Dec 25 '14 at 19:21
  • @πάνταῥεῖ: then maybe actually answer the actual question now, hmm? – Griwes Dec 25 '14 at 19:25
  • @Griwes It wasn't the actual question when posted but edited later. – P.P Dec 25 '14 at 19:27
  • 1
    @BlueMoon: you need a reading lesson: "Is it good practice to use your choice of either/both include guards and #pragma once in every header file, or just those with something such as a class declaration?" <- this was there in the original version of the question! Please, really, guys: start reading. – Griwes Dec 25 '14 at 19:28
  • @Griwes hmm..then there's no question really (when should I use C++?). Perhaps you should read this (now edited) answer too and revoke your downvote. – P.P Dec 25 '14 at 19:34
2

If you want your code to be portable to all C++ compilers you'll need to use include guards. If you feel the compiler you use is inferior and benefits from the use of #pragma once you can also add this pragma: compilers not understanding it will just ignore it. Personally, I don't bother with use of #pragma once. It is a solution to a non-existing problem: compilers can absolutely detect include guards to avoid opening files already included.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • 2
    `#pragma once` solves a real problem, and that real problem is keeping the symbols you define unique. It's not uncommon to encounter files where the defines went out of sync with the file location, and I doubt many people have the patience to use UUIDs for that. There's a single compiler listed on the [Wikipedia page](http://en.wikipedia.org/wiki/Pragma_once#Portability) that doesn't support `#pragma once` and it can't really be considered relevant anymore: "Current (2014) versions of the XL C/C++ compilers support a subset of the C++03 standard on AIX and Linux on Power." – Griwes Dec 25 '14 at 19:19
  • (Subsets of C++03 are pretty much to be considered "random pieces of ancient tech", I'd say.) – Griwes Dec 25 '14 at 19:19
  • 2
    @Griwes: include guards solve the same problem. There is no need for `#pragma once`. ... and just because the wikipedia page choose to list mostly compilers which do support `#pragma once` it doesn't mean there is just this one compiler which doesn't supported. For example, Oracle's CC also doesn't support it (it happens not to be listed). – Dietmar Kühl Dec 25 '14 at 19:22
  • 1
    My point about solving a different problem was about solving the "hmm, what is the unique name for the include guard I should use in this file". – Griwes Dec 25 '14 at 19:23
  • 1
    As for the compilers, I do expect there to be many compilers that do not support this feature, but I do not expect any of those to be anywhere near being relevant or supporting "modern" C++ (namely, *at the very least* C++11; in the perfect world, not supporting C++11 should be a death sentence for a compiler). – Griwes Dec 25 '14 at 19:25
  • @Griwes: yes, some reasonable structure to the include guard name should be applied but that's not a real problem and entirely managable. ... and, yes, some compiler vendors are slower to catch up with the standard but that doesn't mean they are irrelevant. At the place I work at we are using both CC and xlC and we can't use gcc for some or code because gcc can't cope: CC and xlC were more willing to deal with the requirements (I'm not saying that the requirements aren't actually a home-made problem but they are real). – Dietmar Kühl Dec 25 '14 at 19:29
  • All of this is off topic. Please ensure that both the answer and the comments relate to **when** include guards should be used. – Matthew D. Scholefield Dec 25 '14 at 19:49
  • Well, if a compiler doesn't support `#pragma once`, it may be time to upgrade to something more suitable – RecursiveExceptionException Feb 20 '17 at 19:06