Jump to content

  • Log In with Google      Sign In   
  • Create Account


#pragma once


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 Dezachu   Members   -  Reputation: 352

Like
0Likes
Like

Posted 28 September 2012 - 11:45 AM

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 Posted Image

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!

Studying BSc Computer Games Programming @ De Montfort University, Leicester.

On a year long placement as a Junior Programmer at Exient Ltd, Oxford (worked on the recently released Angry Birds Go!) :)


Sponsor:

#2 sheep19   Members   -  Reputation: 388

Like
2Likes
Like

Posted 28 September 2012 - 11:52 AM

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

Edited by sheep19, 28 September 2012 - 11:53 AM.


#3 SiCrane   Moderators   -  Reputation: 9408

Like
9Likes
Like

Posted 28 September 2012 - 12:06 PM

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.

#4 joew   Crossbones+   -  Reputation: 3555

Like
6Likes
Like

Posted 28 September 2012 - 03:08 PM

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.

#5 Pointer2APointer   Members   -  Reputation: 283

Like
0Likes
Like

Posted 28 September 2012 - 03:12 PM

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.

#6 tanzanite7   Members   -  Reputation: 1146

Like
3Likes
Like

Posted 28 September 2012 - 03:17 PM

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.

#7 BinaryPhysics   Members   -  Reputation: 294

Like
-2Likes
Like

Posted 28 September 2012 - 04:32 PM

'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.

Edited by BinaryPhysics, 28 September 2012 - 04:35 PM.


#8 EddieV223   Members   -  Reputation: 1391

Like
0Likes
Like

Posted 28 September 2012 - 05:08 PM

'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-defined 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.

Edited by EddieV223, 28 September 2012 - 05:15 PM.

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.1 Download http://www.sfml-dev.org/download.php

SFML2.1 Tutorials http://www.sfml-dev.org/tutorials/2.1/

 

// 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

 


#9 Slavik81   Members   -  Reputation: 360

Like
5Likes
Like

Posted 28 September 2012 - 06:08 PM

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.

#10 Dezachu   Members   -  Reputation: 352

Like
0Likes
Like

Posted 29 September 2012 - 08:39 AM

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?

Studying BSc Computer Games Programming @ De Montfort University, Leicester.

On a year long placement as a Junior Programmer at Exient Ltd, Oxford (worked on the recently released Angry Birds Go!) :)


#11 superman3275   Crossbones+   -  Reputation: 2017

Like
0Likes
Like

Posted 29 September 2012 - 08:47 AM

Most major compilers support it, and yes, it is efficient. I use it in all my headers to get rid of file problems, and if I run into any trouble, I use forward declarations (That servantofthelord told me about).

I'm a game programmer and computer science ninja ph34r.png!

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

 

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here smile.png!


#12 SiCrane   Moderators   -  Reputation: 9408

Like
1Likes
Like

Posted 29 September 2012 - 09:09 AM

Obviously, #pragma once saves a few characters as compared to traditional include guards, though not enough to make a practical difference in most cases. In theory, #pragma once has a performance advantage over traditional include guards because the preprocessor doesn't reopen headers marked with #pragma once whereas with traditional include guards it needs to reopen the included file and reprocess the preprocessor directives before it can skip the body of the header. In practice, most compiler tool chains can perform the same optimization on include guard protected headers because include guards have a very simply pattern to them.

#13 Dezachu   Members   -  Reputation: 352

Like
0Likes
Like

Posted 29 September 2012 - 09:26 AM

I'll stick with #pragma for now then and if I run into any problems I'll switch to include guards then. Thanks all :)

Studying BSc Computer Games Programming @ De Montfort University, Leicester.

On a year long placement as a Junior Programmer at Exient Ltd, Oxford (worked on the recently released Angry Birds Go!) :)


#14 BinaryPhysics   Members   -  Reputation: 294

Like
0Likes
Like

Posted 29 September 2012 - 07:37 PM

'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.


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


Isn't that exactly what I said? 'pragma' is a pre-processor that allows an implementation to define it's own tokens.

#15 EddieV223   Members   -  Reputation: 1391

Like
0Likes
Like

Posted 29 September 2012 - 11:56 PM

'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.


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


Isn't that exactly what I said? 'pragma' is a pre-processor that allows an implementation to define it's own tokens.


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.

That's the part we are having trouble with. If most compilers support it that makes it rather portable wouldn't you say?

Edited by EddieV223, 30 September 2012 - 12:00 AM.

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.1 Download http://www.sfml-dev.org/download.php

SFML2.1 Tutorials http://www.sfml-dev.org/tutorials/2.1/

 

// 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

 


#16 BinaryPhysics   Members   -  Reputation: 294

Like
0Likes
Like

Posted 01 October 2012 - 12:11 PM

That's the part we are having trouble with. If most compilers support it that makes it rather portable wouldn't you say?


But it's not written as part of the Standard. I was unaware of support external to MSVC++. Surely truly portable code is standard compliant because a compiler isn't required to support it.

#17 BitMaster   Crossbones+   -  Reputation: 3662

Like
0Likes
Like

Posted 02 October 2012 - 01:19 AM

The issue is, what you said was "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."

You could have said it's not part of the standard and thus not guaranteed to work and you are unsure of the support outside of MSVC. That would have gotten your point across without making a wild and false assumption (false assumption have a tendency to annoy people).

Personally, if something is supported by Clang, GCC, Intel and MSVC I consider it standard enough for my purposes as that covers all platforms I have an interest in. I still use both #pragma once and a standard include guard though.

#18 tom_mai78101   Members   -  Reputation: 568

Like
0Likes
Like

Posted 03 October 2012 - 09:27 AM

Wait a minute, I thought "#pragma once" is going to be part of C++11, so that all compilers following C++11 will know of that preprocessor command. Looks like it's not.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS