Author: Chloé Lourseyre
Editor: Peter Fordham
Every C++ developer has been taught header guards. Header guards are a way to prevent a header being included multiple times which would be problematic because it would mean that the variables, function and classes in that header would be defined several times, leading to a compilation error.
Example of a header guard:
#endif // HEADER_FOOBAR
For those who are not familiar with it, here is how it works: the first time the file is included, the macro
HEADER_FOOBAR is not defined. Thus, we enter into the
#ifndef control directive. In there, we defined
HEADER_FOOBAR and the class
FooBar. Later, if we include the file again, since
HEADER_FOOBAR is defined, we don’t enter into the
#ifndef again, so the class
FooBar is not defined a second time.
#pragma is a preprocessor directive providing additional information to the compiler, beyond what is conveyed in the language itself.
Any compiler is free to interpret pragma directive as it wishes. However, over the years, some pragma directives have acquired significant popularity and are now almost-standard (such as
#pragma once, which is the topic of this article, or
#pragma once is a directive that indicates to the compiler to include the file only once. The compiler manages itself how it remembers which files are already included or not.
So, instinctively, we can think that the
#pragma once directive does the job of a header guard, but with only one line and without having to think of a macro name.
In the past, the
#pragma once directive was not implemented for every compiler, so it was less portable than header guards.
But today, in C++, I did not find a single instance of compiler that does not implement this directive.
So why bother using header guards anymore? Answer: because of the issue I’m about to describe.
A strange issues with #pragma once
There is actually one kind of issue that can occur with
#pragma once that cannot occur with header guards.
Say, for instance, that your header file is duplicated somewhere. This is a not-so-uncommon issue that may have multiple causes:
- You messed up a merge and your version control system duplicated some files.
- The version control system messed up the move of some files and they ended up duplicated.
- Your filesystem has two separate mount points that gives a path to the same files. They all appear as two different sets of files (since they are present on both disks).
- Someone duplicates one of your file in another part of the project for its personal use, without renaming anything (this is bad manners, but it happens).
(please note that I encountered each of these four issues at some point in my career).
When this happens, when you have the same file duplicated, header guards and
#pragma once do not behave the same way:
- Since the macros that guard each file have the same name, the header guards will work perfectly fine and only include one file.
- Since, from the FS point of view, files are different,
#pragma oncewill behave as if they are different file, and thus include each file separately. This leads to a compilation error.
Issues with header guards?
Header guards can have issues too. You can have typos in the macro names, rendering your guards useless. That can’t happen with
#pragma once, also it’s possible for macro name to clash if they are badly chosen, also can’t happen with
However, these issues can be easily avoided (typos are easy to detect and name clashes are prevented if you have a good naming convention).
A huge benefit though!
There is also a usage of header guards that is very useful for testing and that is not possible with
Say you want to test the
class Foo (in file
Foo.h) that uses the
class Bar (in file
Bar.h). But, for testing purpose, you want to stub
One option header guards allows you is to create your won mock of
class Bar in file
BarMock.h. If the mock uses the same headers guards than the original
Bar, then in you test, when you include
Foo.h, the header
Bar.h will not be included (because the mock is already included and has the same guards).
So, should I use #pragma once or header guards?
This question is a bit difficult to answer. Let’s take a look at the cons of each method:
#pragma onceare non-standard and are a major issue when you end up in a degraded environment.
- Header guards may have issues if handled improperly.
In my opinion,
#pragma directives are to be avoid when possible. If, in practice, they work, they are not formally standard.
Dear C++20, what about Modules?
Modules, one of the “big four” features of C++20, changes our vision of the “classical build process”. Instead of having source and header files, we can now have modules. They overcome the restrictions of header files and promise a lot: faster build-times, fewer violations of the One-Definition-Rule, less usage of the preprocessor.
Thanks to modules, we can say that
#pragma once and header guards issues are no more.
To learn more about modules, check these out:
This article, talking about pragmas and header guards, targets project that are prior to C++20. If you are in this case and hesitate between
#pragma once and header guards, maybe it’s time to upgrade to C++20?
If you can’t upgrade to C++20 (few industrial project can), then choose wisely between
#pragma once and header guards,
Thanks for reading and see you next time!
Author: Chloé Lourseyre
Editor: Peter Fordham