Take the 2-minute tour ×

Possible Duplicate:
#pragma once vs include guards?

What are the differences (in performance, usability and functionality) in using #pragma once and #if !defined MYHEADER_INCLUDED_ constructs? Or what is the difference between the two?

share|improve this question

marked as duplicate by casperOne Aug 25 '12 at 18:11


add comment

7 Answers

up vote 9 down vote accepted

Wikipedia has all your answers for this and is easy enough to find

From the article

In the C and C++ programming languages, #pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation.

Thus, #pragma once serves the same purpose as #include guards, but with several advantages, including: less code, avoiding name clashes, and improved compile speed.

It has a more in-depth look at the advantages and disadvantages in the article. If you are really interested I suggest you read it completely, not just the blurb above.

share|improve this answer
:) thanks, let me read the article! –  bdhar Jun 3 '10 at 7:56
Thanks Donal, I've been having issues when using the block quote button lately so I've been tending to avoid it. I shouldn't be so lazy and just do it manually... –  Dan McGrath Jun 3 '10 at 12:37
add comment
  1. performance -- usually the #pragma once implementation compiles faster, because it's intent is clear
  2. functionality -- both serve the same purpose -- to avoid more than one inclusion of a header file. The defined version has of course wider uses too, and if used carefully will allow to check in another file if the former was included (for conditional compilation for example)
  3. usability -- if portability is not an issue go with #pragma once -- however, this is a non-standard extension
share|improve this answer
The day #pragma once becomes standard, my life will become a little bit better. –  mizipzor Jun 3 '10 at 8:32
@mizipzor, I want no stinkin' #pragma's, I want MODULES! open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2073.pdf :D –  Kornel Kisielewicz Jun 3 '10 at 8:40
The day #pragma once becomes unnecessary, my life will become a little bit better. –  Dennis Zickefoose Jun 3 '10 at 8:41
@Dennis: Touché –  mizipzor Jun 3 '10 at 9:07
+1 for mentioning that it's not standard –  Geoff Jun 3 '10 at 12:50
add comment

While pragma once is superior in many aspects to #ifdef guards, it has one major flaw: it is based on file system paths: it uses the basic assumption that two different (absolute) paths are also two different files. Unfortunately this is not always true.

While it is not very likely you'll run into problem with this, it is something to be aware of.

share|improve this answer
add comment

Some more background on this:

share|improve this answer
add comment

As far as performance goes, I haven't measured it, but it's widely reported that compilers recognize the #include guard idiom and do not reopen/reprocess files that use it (therefore treating them the same as having used #pragma once).

Other than that potential performance benefit (which probably doesn't exist), I see little that #pragma once offers - it's somewhat more convenient when you create a new header - but guards really aren't that onerous to put in place. Since #pragma once isn't particularly compelling and since I still occasionally deal with a compiler that doesn't support it, I use guards.

If it was shown that #pragma once had a significant performance effect, I'd certainly start using it.

That said - I wish that C had been defined to only include headers once - unless some mechanism was invoked to indicate the header should be processed each time it was included. The times when you want that behavior are far, far out numbered by the times you don't (and I still come across headers every now and again that don't have guards or #pragma once). But it wasn't defined that way, so include guards it is...

share|improve this answer
add comment

As Dan McG said, they are almost the same. But I don't recommend using #pragma unless in the cant-avoid things (like #pragma section) .It just make the code more non-standard.

And there is no functionality/performance issue. Because this is in compiling time. It might increase compiling time, but I didn't experience any difference!

share|improve this answer
add comment

#pragma once is non standard.

If you're happy with the prospect of changing every single one of your header files when you discover your app or library won't build when you try to port it to a different platform, it's probably a good choice.

Otherwise, stick with #ifndef/#define/#endif. Especially if you're building library code for other people to use.

share|improve this answer
add comment
