#pragma once

Started by
16 comments, last by tom_mai78101 11 years, 6 months ago
Hi hi guys :)

Going into my second year of uni and it's about time I really knuckled down, endured many headaches and cracked on with some C++!

Quick question about '#pragma once'. I know it makes sure a header file is only included once in the compilation. Well, source files, not strictly headers ph34r.png

Anyway, I wanted to know if it's worth including this in EVERY header file I write, or is there a particular reason one may wish to repeatedly include a particular file?


Thanks guys!

BSc Computer Games Programming (De Montfort University) Graduate

Worked on Angry Birds Go! at Exient Ltd

Co-founder of Stormburst Studios

Advertisement
1) Do not use it in source files. Source files are not "included" anyway.
2) You should use it in every header file. If you don't, you are just asking for trouble. And no, there is no reason to include a header file more than once.
3) By using #pragma once you are not being "portable". That means your program is going to work only on windows, on the compiler that comes with visual studio (cl).
The correct (i.e. portable) way is to do it like this:
#ifndef SOME_FILE_H
#define SOME_FILE_H

// all your definitions here

#endif
There are times when you want a header body to be included more than once so wouldn't use include guards or pragma once. However, if you're posting in For Beginners, then you don't need to worry about those situations for a while longer. Most of the situations where you would want to do something like that involve preprocessor metaprogramming such as with boost::preprocessor. Another example of a header without include guards is the standard C library header assert.h and the standard C++ library header cassert. You can disable or enable asserts for a region of code by #defining or #undefing NDEBUG and reincluding those headers.

3) By using #pragma once you are not being "portable". That means your program is going to work only on windows, on the compiler that comes with visual studio (cl).

Slightly incorrect. Quite honestly I can't think of a single compiler worth using that doesn't respect #pragma once... and I've used it with GCC and Clang all the time. The reason I say "slightly" is because on some oddball obscure platform with a strange compiler there may not be support for it, and that is when you need to use the header include guards instead.
Not "every" header file, but probably most.

Preprocessor metaprogramming is something I've never tried before, but the above user seems to know more of it than me, obviously.
Yes, this is red text.

3) By using #pragma once you are not being "portable". That means your program is going to work only on windows, on the compiler that comes with visual studio (cl).
The correct (i.e. portable) way is to do it like this:
#ifndef SOME_FILE_H
etc

Yes and no.

1. "pragma once" is not as badly supported as portrayed (http://en.wikipedia.org/wiki/Pragma_once).
2. it is more straightforward and is trivial to "fix" (as it is superbly unambiguous) if target implementation does not support it.
3. i would welcome more pressure for other compiles to support it too as not supporting it is embarrassing.

If one makes some library for many platforms/compilers then one should use the old kind of guards tho.
'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience.

'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience.


#pragma is standard, the uses for it such as 'once' are not. It is a way to provide additional information.

However #pragma once is supported by MOST compilers worth using. Also using #pragma once instead of regular include guards can speed up compilation because it allows skipping of the file instead of parsing it for include guards.

So no it isn't a Visual studio only thing.

From the c++11 standard

16.6 Pragma directive [cpp.pragma]
1 A preprocessing directive of the form
# pragma pp-tokens opt new-line
causes the implementation to behave in an implementation-de?ned manner. The behavior might cause
translation to fail or cause the translator or the resulting program to behave in a non-conforming manner.
Any pragma that is not recognized by the implementation is ignored.

-------------------------------------------------------------

If you're paranoid you can use both, with out causing any errors
#pragma once
#ifndef MYFILE_H
#define MYFILE_H

#endif

The compiler will use the pragma if supported, if not, it will just ignore it and continue parsing.

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

I would use #pragma once because it's simpler and leaves less room for programmer error. It is supported across all major compilers (gcc, msvc, clang, intel...). In the event that you discover that you need to support an old compiler, you can run a script such as this one to replace #pragma once with include guards.

I've only once encountered a header file in which I wouldn't want to include #pragma once. By default, you should be doing it for every header file.
Wow, cheers for all the replies guys :)

Basically #ifndef is understood by more compilers, but pragma is more efficient? My programs aren't going to be HUGE as I'm only just learning - I guess ifndef is better to work with for now?

BSc Computer Games Programming (De Montfort University) Graduate

Worked on Angry Birds Go! at Exient Ltd

Co-founder of Stormburst Studios

This topic is closed to new replies.

Advertisement