Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: #pragma once vs #ifndef - Which is better?  (Read 36811 times)

0 Members and 1 Guest are viewing this topic.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
#pragma once vs #ifndef - Which is better?
« on: January 23, 2014, 03:02:33 pm »
What is the difference between #pragma once and #ifndef guards? Which is better to use and why?

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: #pragma once vs #ifndef - Which is better?
« Reply #1 on: January 23, 2014, 03:05:54 pm »
#pragma isn't recognize by all compilers. I do not have a list but they tend to include it more and more.

AFAIK there is no differences but #pragma once is definitively more practical :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: #pragma once vs #ifndef - Which is better?
« Reply #2 on: January 23, 2014, 03:07:33 pm »
#pragma once is more efficient because the preprocessor can totally skip the header file (it doesn't have to read its contents). But it is only understood by Visual C++.

So unless you have a huge project and it makes a difference on the compilation time, use #ifdef guards instead.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: #pragma once vs #ifndef - Which is better?
« Reply #3 on: January 23, 2014, 03:31:33 pm »
Meanwhile, there are a lot of compilers that understand #pragma once. Most modern and relevant compilers support it, at least VC++, g++, clang, Intel.

To be on the safe side, you should still prefer the classical header guards -- #pragma once is not C++ standard, and no compiler is forced to support it.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: #pragma once vs #ifndef - Which is better?
« Reply #4 on: January 23, 2014, 03:37:01 pm »
Quote
#pragma once is more efficient (...) But it is only understood by Visual C++.
Seriously Laurent?
http://en.wikipedia.org/wiki/Pragma_once#Portability

Just pick one and use it, it doesn't matter at all. If you move to a compiler that doesn't have pragma once then you can just write a super simple script that will create include guards for you. Include guards are guaranteed to work by standard but might be slower. Some compilers detect if file has include guard and don't reopen it then constantly.
If you are really paranoid about include guards being slower and pragma not being implemented you can use both, unknown pragmas* are ignored and if pragma once fails then include guards do the work.
I use include guards since it doesn't matter in GCC anyway and NetBeans writes them and a creation comment in each header for me automatically.
Also without include guards header files can't prevent each other from including, like glew.h prevents gl.h from being included but that is rarely needed.

*From 16.6.1 in last c++ standard:
Quote
A preprocessing directive of the form
# pragma pp-tokensopt 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.
« Last Edit: January 23, 2014, 03:40:48 pm by FRex »
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: #pragma once vs #ifndef - Which is better?
« Reply #5 on: January 23, 2014, 03:58:53 pm »
My personal preference is:

For libraries, I'll use #ifndef or a mix of both, just to make sure the code is standard conform.
For my own applications/games, I'll use #pragma, because I know which compiler is going to be used and if I make it open source and people have problems with their own compiler, it's not really something I have to fix, since the code was never intended to work for every possible setup. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: #pragma once vs #ifndef - Which is better?
« Reply #6 on: January 23, 2014, 04:02:37 pm »
Quote
Seriously Laurent?
It was true some time ago. Seems like I need an update ;D
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: Re: #pragma once vs #ifndef - Which is better?
« Reply #7 on: January 23, 2014, 09:57:32 pm »
It was true some time ago. Seems like I need an update ;D
Maybe you were confused, by the fact that GCC once accidentally marked #pragma once as deprecated in their manual. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Assassin0795

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: #pragma once vs #ifndef - Which is better?
« Reply #8 on: January 24, 2014, 01:14:16 am »
Not sure if it counts for anything, but what I was taught was to use #ifndef - might have been because we used GCC at the time, though. I use #Pragma once if I know I'm the only one who will be looking/working on the project.

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: #pragma once vs #ifndef - Which is better?
« Reply #9 on: January 24, 2014, 05:09:11 pm »
Keeping the code standards-conformant is always better than not, when there is no huge gain otherwise.
I read a test some time ago that showed some compilers would actually optimize the normal header guards with #ifndef to not open the header file twice. In that case the #pragma extension would get useless anyway, which would explain it getting deprecated.
Btw., completely removing #include statements by forward declaring types in headers may be more useful in reducing compile time.
And maybe in xx years C++ gets modules. :P
« Last Edit: January 24, 2014, 05:15:23 pm by wintertime »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: Re: #pragma once vs #ifndef - Which is better?
« Reply #10 on: January 24, 2014, 07:27:10 pm »
Keeping the code standards-conformant is always better than not, when there is no huge gain otherwise.
The "gain" between the two is mostly personal preference, though statistically removing 2/3 of the lines and preventing include guards redefinition (i.e. if two headers use the same guard), is quite significant. ;)

I read a test some time ago that showed some compilers would actually optimize the normal header guards with #ifndef to not open the header file twice. In that case the #pragma extension would get useless anyway
Compilers are always changing so if it was sometime ago, it's likely that it's not true anymore.
Besides of one gets optimized it doesn't automatically render the other useless. It could be optimized as well. ;)

which would explain it getting deprecated.
You didn't read carefully enough, it was by accident. ;)

Btw., completely removing #include statements by forward declaring types in headers may be more useful in reducing compile time.
Forward declaration should always be used where possible, regardless of the include guards, in fact it has nothing to do with it. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: AW: Re: #pragma once vs #ifndef - Which is better?
« Reply #11 on: January 24, 2014, 07:44:37 pm »
Forward declaration should always be used where possible, regardless of the include guards, in fact it has nothing to do with it. ;)

What do you mean by this?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: AW: Re: #pragma once vs #ifndef - Which is better?
« Reply #12 on: January 24, 2014, 08:58:15 pm »
What do you mean by this?
If the code doesn't require the definition of a type or function, then it's enough to declare it (and not define it) before it is used. This often means you don't need to include headers.

See also GotW #7
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: AW: Re: #pragma once vs #ifndef - Which is better?
« Reply #13 on: January 24, 2014, 11:48:55 pm »
What do you mean by this?
If the code doesn't require the definition of a type or function, then it's enough to declare it (and not define it) before it is used. This often means you don't need to include headers.

See also GotW #7

Does this just affect compile times or does it also affect actual program performance at run time? I imagine this just applies to compile times.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: #pragma once vs #ifndef - Which is better?
« Reply #14 on: January 25, 2014, 12:11:39 am »
Yes, it's just compile times. And it also speeds up tools that perform static code analysis (auto completion, real-time error messages, dependency graphs, doxygen, ...).

(There can be rare cases where it actually affects runtime behavior, such as static/const variables in headers that are not optimized away, symbols that participate in ADL or overload resolution, or macros that change the compilation. But the primary goal is to reduce compile times.)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything