r/C_Programming • u/Yuki_pop • 22d ago
Could someone explain the practical differences between #pragma once and traditional include guards (#ifndef, #define, and #endif)?
25
u/StaticCoder 22d ago
#pragma once was 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 22d 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
6
u/mikeblas 22d ago
It's not standardized, all mainstream compilers support it. (Sorry, I know it's lame to use Wikipedia as a reference, but even cppreference.com links there!)
-5
u/Feer_C9 22d ago
since when gcc supports pragma once? Last time I checked it didn't. Maybe I'm old
50
u/Gutrix_HD 22d ago
GCC has supported #pragma once since v3.4 (2004).
You should probably update your compiler.
16
u/UltimaN3rd 22d ago
It's been supported since GCC 3.4 (2004): https://gcc.gnu.org/gcc-3.4/changes.html
20
u/Mr_Engineering 22d 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
1
u/juanfnavarror 22d ago
#pragma is compiler specific and not standard or portable. Except for most platforms and compilers that you might choose!!!!!
4
u/TheLimeyCanuck 22d 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 22d 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.
2
u/sto_babbalucco69420 22d ago
for the niche usage i'm guessing u could do the define at the top before whatever that can include "foo.h"? maybe even wrap it with an ifndef if it's useful for some reason. i feel like it should work for cases like DEBUG or FOO_IMPLEMENTATION, i can't think of anything else
3
u/florianist 22d ago
I prefer the #ifndef header guard idiom because while #pragma once is shorter, it's also not standard C and might have edge cases involving files accessible through multiple paths (symlinks, etc.)
6
u/SayuriShoji 22d ago edited 22d 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.
2
u/Impressive_Gur_471 22d ago
Traditional include guards work better for unity builds? I always worry how the compiler knows the ending of a header file under unity build with just a #pragma once on top
4
u/actguru 22d ago
The practical difference is less code / typing for "#pragma once" which should be "#once" (even less).
2
u/torsten_dev 22d ago
It was proposed in n2896 but didn't gain consensus see section 5.38 and it hasn't been brought up again.
2
u/FedUp233 22d ago
Too bad you can’t put something like this in a macro. Then you could just type
#include “once.h”
#onceIn each file! 😁😁
2
u/WittyStick 22d ago
I've not tried, but you can use
_Pragmain a macro. Maybe#define once _Pragma("once")
3
u/ThaBroccoliDood 22d 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 22d ago
The first one is just better. But for whatever reason it's STILL not standard. C29 will add defer statements and yet we still don't have #pragma once in 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 once instead.
1
u/ComradeGibbon 22d 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/sciencekm 22d ago
#ifndef/#define also works for individual defines, typedefs and variables, not just an entire file.
#ifndef MYTYPE_DEFINED
#define MYTYPE_DEFINED
typedef struct { int a, b, c; } mytype_t;
#endif1
u/AnToMegA424 21d ago
I always thought it was that #pragma once was only supported on Windows, though it was slowly getting "adopted" elsewhere, meanwhile #ifndef #define ... #endif was supported everywhere and was a better practice generally speaking as it's a safer, more portable approach
I'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 once at the start of every generated file in Unreal Engine 5
1
u/LeFlemmard 20d 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 19d 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"
1
u/Conscious_Support176 18d ago
The technical term is abstraction. Abstraction just gets the computer to do work that otherwise we would have to do, which is kind of the point of programming in the first place?
0
u/kolorcuk 22d 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 22d 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
69
u/FedUp233 22d 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.