• 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
wicked357

#pragma once vs #ifndef

14 posts in this topic

I have been reading up on the two differences and require a little conversation to get a better idea as to why I would use one over the other since what I find isn't that helpful. Mainly why I would use #pragma once over #ifndef or both together. It appears that using both would be benificial as well, taking advantage of any optimizations the compiler may have and for writing portable code. A better insight on this would be appreciated.
0

Share this post


Link to post
Share on other sites
"#pragma once" is non-standard, meaning the compiler is absolutely allowed to ignore it, so IMO you should use "#pragma once" first (due to it being a compiler optimisation), but also use include guards (#ifndef x, #define x, #endif) as a backup.

In reality though, every modern C++ compiler [i]does[/i] support "#pragma once", so it's pretty safe to leave out the traditional include guards.
1

Share this post


Link to post
Share on other sites
#pragma once is not, strictly speaking, portable. #ifndef guards usually are, if properly implemented (that is if you're not using reserved symbols for the guard). It doesn't really matter in general. I almost never use #pragma once.
1

Share this post


Link to post
Share on other sites
[quote]IMO you should use "#pragma once" first (due to it being a compiler optimisation)[/quote]

Is it an optimisation though? Last time I investigated, MSVC was just as fast with portable scope guards as it was with #pragma once. I presume they accomplish that by applying the same optimization to scope guards as they do to #pragma once (it's essentially the same, with the additional detail that you have to store a mapping of file paths to scope guard names rather than just file paths - but it isn't tricky).
0

Share this post


Link to post
Share on other sites
It is an optimisation, but only technically.

The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.

In practice it's never made a noticeable difference ime and I've worked on fairly large projects using both methods. Nowadays we use the traditional way, as it's absolutely guaranteed to still work on some of the crappier compilers our work often sees us faced with.After all, it's hardly a hassle.
0

Share this post


Link to post
Share on other sites
[quote name='Rubicon' timestamp='1295428062' post='4761142']
It is an optimisation, but only technically.

The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.

In practice it's never made a noticeable difference ime and I've worked on fairly large projects using both methods. Nowadays we use the traditional way, as it's absolutely guaranteed to still work on some of the crappier compilers our work often sees us faced with.After all, it's hardly a hassle.
[/quote]

That's right. There's an article out there with numbers. The difference between #ifdef and #pragma once is not noticeable and often not even measurable.
More effective ways to speed up compilations times are:
* Minimizing your header dependencies
* Precompiled headers
* Big cpp method (#include all .cpps into one big .cpp and compile that. Speeds up linking as well, but only good for complete rebuilds and may have side effects when depending on compilation unit scopes)
* ccache or similar
* distributed compilation
0

Share this post


Link to post
Share on other sites
Portability is only one issue. What is more important for me is that #pragma once doesn't allow including a very certain file twice given its location. This little detail may seem unimportant, but let's just imagine you are using some version control that allows sharing files among different directories. It is then tempting to copy some commonly used headers to different modules of the application (different projects in MSVC terminology). Now just imagine following situation:

file A.cpp includes some shared header from A location - G.h, but also includes some header from other module (fe. DLL) - D.h.
Unfortunately file D.h includes the same shared header, but from D location - G.h
Please note that G.h is finally included twice when compiling A.cpp, but each time from a different location, so they are 2 distinct files on a hard drive.

In such a case using #pragma once will result in including this G.h file twice and probably resulting in some redefinition errors, while using #ifndef will work as expected (even though they are 2 distinct files, their content is the same, so name of preprocessor definition is exactly the same).

That's generally why I have switched to using #ifndef... #define... #endif guards from #pragma once
0

Share this post


Link to post
Share on other sites
[quote name='Rattenhirn' timestamp='1295432898' post='4761171']
[quote name='Rubicon' timestamp='1295428062' post='4761142']
It is an optimisation, but only technically.

The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.

In practice it's never made a noticeable difference ime and I've worked on fairly large projects using both methods. Nowadays we use the traditional way, as it's absolutely guaranteed to still work on some of the crappier compilers our work often sees us faced with.After all, it's hardly a hassle.
[/quote]

That's right. There's an article out there with numbers. The difference between #ifdef and #pragma once is not noticeable and often not even measurable.
More effective ways to speed up compilations times are:
* Minimizing your header dependencies
* Precompiled headers
* Big cpp method (#include all .cpps into one big .cpp and compile that. Speeds up linking as well, but only good for complete rebuilds and may have side effects when depending on compilation unit scopes)
* ccache or similar
* distributed compilation
[/quote]

gcc claims to have optimized code for traditional include guards, and I have no reason to not believe it. So if there is a big difference, then prolly on compilers that don't optimize #ifndef-guards :)
0

Share this post


Link to post
Share on other sites
[quote name='Pomnico' timestamp='1295437102' post='4761195']
Portability is only one issue. What is more important for me is that #pragma once doesn't allow including a very certain file twice given its location. This little detail may seem unimportant, but let's just imagine you are using some version control that allows sharing files among different directories. It is then tempting to copy some commonly used headers to different modules of the application (different projects in MSVC terminology). Now just imagine following situation:

file A.cpp includes some shared header from A location - G.h, but also includes some header from other module (fe. DLL) - D.h.
Unfortunately file D.h includes the same shared header, but from D location - G.h
Please note that G.h is finally included twice when compiling A.cpp, but each time from a different location, so they are 2 distinct files on a hard drive.

In such a case using #pragma once will result in including this G.h file twice and probably resulting in some redefinition errors, while using #ifndef will work as expected (even though they are 2 distinct files, their content is the same, so name of preprocessor definition is exactly the same).

That's generally why I have switched to using #ifndef... #define... #endif guards from #pragma once
[/quote]

A much better solution is to move common headers into a "shared" path such that you can therefore keep track of where exactly you reuse such files. This also helps clean up source control a bit, and makes maintaining compatibility between APIs/clients much easier, since any change in the shared path is guaranteed to break all clients.
0

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1295437848' post='4761207']
A much better solution is to move common headers into a "shared" path such that you can therefore keep track of where exactly you reuse such files. This also helps clean up source control a bit, and makes maintaining compatibility between APIs/clients much easier, since any change in the shared path is guaranteed to break all clients.
[/quote]
I definitely agree, just wanted to point out that there can be such approach to code organization due to for example SourceSafe possibilities (seems quite common from my own experience despite the fact I think it's messy) and #pragma once just doesn't go together with it.
0

Share this post


Link to post
Share on other sites
[quote]The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.[/quote]
That's wrong. The speed gain attained by the optimization is due to the fact that the file doesn't have to be opened a second time. It's the file access, not the parsing, that is the performance problem. Parsing takes a trivial amount of time compared to file I/O.

The optimization works like this (pseduo code)
[html]
FOR EACH translation unit
FOR EACH #include directive encountered
IF the file is in the set of "#pragma once" files
OR the file is in the set of files with include guards and the relevent preprocessor symbol is still defined:
We're finished processing the include (no need to open the file at all)
ELSE:
Process the file
If the file contained a pragma once, add it to the set of "#pragma once" files
If the file contained an include guard add it to the set of files with include guards,
along with the record of the relevent preprocessor symbol
[/html]

The important things to note are:
* The optimization works just as well with #pragma once as it does with include guards - it's slightly easier to implement with #pragma once, which is why the #pragma once optimization precedes the optimization being applied to normal include guards.
* An #include directive does not require the included file to be opened and read a second time, cutting down on file IO.
0

Share this post


Link to post
Share on other sites
[quote name='Nitage' timestamp='1295444459' post='4761238']
[quote]The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.[/quote]
That's wrong. The speed gain attained by the optimization is due to the fact that the file doesn't have to be opened a second time. It's the file access, not the parsing, that is the performance problem. Parsing takes a trivial amount of time compared to file I/O.
[/quote]
Yes, you're right of course. I blame that brain fart on having just got up! :)

It still doesn't make any difference in my world. In fact, in my standard non-standard ways that Apoqpiq loves so much, I get the most speed boost by breaking ALL the rules.

We have a single header file that includes all the others. This single file is made into a PCH. No other source files include anything other than this one master file. Which sounds about as suboptimal as you can be, right? Especially when you touch a single header and have to suffer a full rebuild for your trouble.

But, I urge anyone to at least try it one wet weekend. If you stick rigidly to the principal of including this ONE file only in all your sources, the speed boost you see will leave you slack jawed and you'll suddenly understand that a full rebuild each time you touch a loose header really isn't a problem when it only takes several seconds.

EDIT: Dunno if this works on gcc too well, depends on the version. DevStudio fair zings along, but the biggest win we ever had was switching someone elses project (done in the usual way) to our uber-header method when porting to DS. The PCH speed-up in CodeWarrior is ridiculous. A full rebuild of defcon took a couple of seconds, I shit you not.
0

Share this post


Link to post
Share on other sites
My pattern looks like this:
[code]
#ifndef FOO
#define FOO
#pragma once
// ... header stuff ...
#endif
[/code]
Compiler can choose which one it likes best :) The big downside to #pragma is that you can't include the same header more than once in a translation unit, which I've seen used in some hacky situations:
[code]
#include "Foo.h"

#include "Bar.h"

// Hacky fun time
#undef FOO
#include "Foo.h"
[/code]
But this is an exception to the rule and certainly should be handled on a case-by-case basis for any header that needs to do this.
0

Share this post


Link to post
Share on other sites
[quote]Compiler can choose which one it likes best The big downside to #pragma is that you can't include the same header more than once in a translation unit, which I've seen used in some hacky situations:[/quote]

I wouldn't call this a downside. #pragma once is designed specifically to call a header [b]once[/b], without the rather inelegant method of 3 separate # directives and having to make sure you're using a unique symbol.
If you need to call a header twice in the same translation unit then by all means use something other than the compiler directive designed specifically not to call the same header twice.


I use it in MSVC and it apparently works on the other major compilers too. If a feature is supported on the compilers you're using then I don't see a reason not to use it. While it's not supported on *every* compiler, the same could be said about several other c++ features which are standard compliant and *still* have poor compiler support.

0

Share this post


Link to post
Share on other sites
#pragma once is pretty much safe to use cross compiler nowadays. If you want to use it you can. I think it was add in GCC 4.0.0 maybe earlier. The new compiler Clang supports it also afaik for Mac.
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