• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Dezachu

#pragma once

17 posts in this topic

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 [img]http://public.gamedev.net//public/style_emoticons/default/ph34r.png[/img]

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!
0

Share this post


Link to post
Share on other sites
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
2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
'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
-2

Share this post


Link to post
Share on other sites
[quote name='BinaryPhysics' timestamp='1348871544' post='4984884']
'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.
[/quote]

#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. Edited by EddieV223
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
I'll stick with #pragma for now then and if I run into any problems I'll switch to include guards then. Thanks all :)
0

Share this post


Link to post
Share on other sites
[quote name='BinaryPhysics' timestamp='1348871544' post='4984884']'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.[/quote]

[quote name='EddieV223' timestamp='1348873695' post='4984891']#pragma is standard, the uses for it such as 'once' are not. It is a way to provide additional information.[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='BinaryPhysics' timestamp='1348969079' post='4985216']
[quote name='BinaryPhysics' timestamp='1348871544' post='4984884']'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.[/quote]

[quote name='EddieV223' timestamp='1348873695' post='4984891']#pragma is standard, the uses for it such as 'once' are not. It is a way to provide additional information.[/quote]

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

[quote name='BinaryPhysics']
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.
[/quote]
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
0

Share this post


Link to post
Share on other sites
[quote name='EddieV223' timestamp='1348984586' post='4985275']That's the part we are having trouble with. If most compilers support it that makes it rather portable wouldn't you say?[/quote]

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

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0