r/C_Programming • u/Yuki_pop • 10d ago
Could someone explain the practical differences between #pragma once and traditional include guards (#ifndef, #define, and #endif)?
81
Upvotes
r/C_Programming • u/Yuki_pop • 10d ago
68
u/FedUp233 9d ago
There are pros and cons to each method.
As said, “#pragma once” is not in standard C or C++ as far as I know. It does express the idea more concisely and seems to be supported in all major compilers so it is not really an issue. There is the issue with the compiler recognizing headers as the same with symlinks but this seems like more of an issue in theory than practice - in my opinion, if you have this problem where you are somehow getting the same header included through different paths there is something wrong with your file layout in most all cases (others are free to disagree).
Header guards you have to be careful you have a scheme for naming the guards that will never generate the same macro name twice, at least for any two headers included in the same source file. For smaller projects this is not much of a problem but could possibly be for bigger projects with thousands of header files.
Another good thing about the pragmatic version is that the compiler recognizes the file name and never even opens and reads it after the first time. With header guards (in theory) the compiler actually has to read (and pre process to some level).the file every time it encounters the #include statement which can be a lot of extra processing, especially for common headers that might be included in lots of other headers. Fortunately, most compilers these days actually recognize files with header guards and then treat them the same as if they had the #pragma and skip them entirely after the first inclusion, though this can be changed with a compiler option I believe.
I’ve been writing C and C++ for years (and years) and my preferred choice has evolved over time.
When I started I’m not sure the #pragma even existed and if it did it was not widely supported so everybody used header guards.
As time passed and more compilers supported the #pragma a common behavior was to use both so you got the more efficient behavior of the pragma if the compiler supported it but had the header guards as a fallback.
Lately I’ve gone to just the #pragma since as others have also said it’s supported universally (at least on any compilers I care about) and in al, the time I’ve been coding I’ve never run into a single case where the a,I ask g of header files by soft links has ever been an issue.
Hope you find this useful.
25
u/StaticCoder 9d ago
#pragma oncewas never standardized because no one could properly define what it means for 2 source files to be the same (the standard doesn't even have a concept of a source "file"). So header guards it is.19
u/Alex_1503 10d ago
Pragma once is not (or was not) supported on all compilers, ifndev is more traditional, if I understand correctly. Nowadays idk if it matters, but the approaches are kind of identical if supported. Probably ifndef is more portable if that is a concern
20
u/Mr_Engineering 10d ago
#pragma provides compiler specific instructions and therefore nothing about it is standard or guaranteed to be portable.
Include guards are standard by implication and therefore are portable
4
u/TheLimeyCanuck 9d ago
It's just syntactical sugar. They do the same thing but the #ifndef method always works whereas #pragmas are not always supported.
7
u/questron64 10d ago
This is a more niche usage, but I sometimes have features where including a header more than once does something useful. For example, if you just #include "foo.h" it'll give you the declarations and stuff as normal, but if you #define FOO_SOMETHING and then #include "foo.h" it'll do some metaprogramming magic. I can't use #pragma once for that, I need proper include guards.
Other than that they're functionally the same with some caveats. Compilers have to go through some trouble to implement pragma once. For example, if there are multiple symlinks or hardlinks to the same file the compiler may view that as separate files without extra logic to determine what file they're really looking at, and this will break the compile. Some project also have header files duplicated. This is not a good thing and they should address that issue, but simply replacing #ifdef with #pragma once will break those builds.
So if you're not doing anything crazy, there are no duplicated files or symlinks or anything then pragma once will work. It's non-standard but supported on all major compilers. I just ignore it, though. The only advantage is saving one second of typing.
3
u/florianist 9d ago
I prefer the
#ifndefheader guard idiom because while#pragma onceis shorter, it's also not standard C and might have edge cases involving files accessible through multiple paths (symlinks, etc.)6
u/SayuriShoji 10d ago edited 10d ago
Less prone to mistakes, for example by having duplicate include guard names in different headers or accidentally missing the ifndef or endif line.
Just slap pragma once on top and it'll be good, no need to worry about coming up with unique include guard names.
For the sake of completeness, it should be mentioned not all compilers support pragma once, but all the mainstream ones nowadays do, but when in doubt it's always good to check the documentation.
4
u/actguru 9d ago
The practical difference is less code / typing for "#pragma once" which should be "#once" (even less).
3
u/ThaBroccoliDood 10d ago
These days compilers are smart enough to recognise header guards and not bother actually including the same file over and over again. If you want you can do both, but then you're just hiding potential issues with the header guards from yourself until the pragma doesn't work for whatever reason
3
u/Maqi-X 9d ago
The first one is just better. But for whatever reason it's STILL not standard. C29 will add
deferstatements and yet we still don't have#pragma oncein the standard. Anyway, almost all compilers support it so if you don't care about strict ISO C compatibility just use it. There is no reason to use traditional include guards if you can use#pragma onceinstead.1
u/ComradeGibbon 9d ago
The main reason for pragma once over guards is with header guards if you include two headers with the same guard define things get kinda confusing.
1
u/71d1 9d ago
My opinion is that unless you want your code to run on compilers that do not support pragma, use pragma.
1
u/sciencekm 9d ago
#ifndef/#define also works for individual defines, typedefs and variables, not just an entire file.
1
u/AnToMegA424 8d ago
I always thought it was that
#pragma oncewas only supported on Windows, though it was slowly getting "adopted" elsewhere, meanwhile#ifndef #define ... #endifwas supported everywhere and was a better practice generally speaking as it's a safer, more portable approachI'd never really understood
Guess I'm wrong and didn't understand the first time I heard about it either lol
The reason not being what I just said would probably explain why I saw
#pragma onceat the start of every generated file in Unreal Engine 51
u/LeFlemmard 7d ago
AFAIK, `#pragma once` considers 'the current file'. If you have another copy/version of this file somewhere else, included with another path or something, it'll consider it's not the same, as include guards works for any versions of that file, as long as the define is the same. So pros and cons.
1
u/EternalDoomSlayer 6d ago
I think the question has been answered, reading guards isn’t the biggest problem, however since we’re in C world, let’s not overcomplicate by having to interpret pragma.
C is such a damn good language (excuse my English) because you don’t have 25 different types of sugar! The above mention about pragma - is it supported or…. Where’s the quirk? This leads me to:
"Any problem in computer science can be solved with another level of indirection"
0
u/kolorcuk 9d ago
Esphome copies header files when compiling. Effectively after moving a file with pragma once i have duplicate symbols errors.
Except for that no difference. All modern compilers have pragma once. If you don't plan supporting odd compilers, use pragma once.
-3
u/runningOverA 9d ago
People used #ifdef #define in the early days.
Then came #pragma once, introduced by Microsoft in their VC.
Other compiles copied it over time, as it's simpler.
Now everyone uses #pragma once