A header guard is a short set of preprocessor lines that keeps a header’s contents from being processed more than once in the same source file. You place it at the top and bottom of each header. The same pattern works for a header guard in C, so the advice here applies to both languages.
Why Headers Can Be Included More Than Once
A header is pulled into a source file with #include. The trouble starts when two headers both include a third one. Consider this small header file example, which has no guard yet:
// shapes.h
struct Point {
int x;
int y;
};
// geometry.h
#include "shapes.h"
double distance(Point a, Point b);
// main.cpp
#include "shapes.h"
#include "geometry.h"
int main() {
return 0;
}
When the compiler reads main.cpp, it includes shapes.h directly, then includes it again through geometry.h. It now sees two definitions of Point, and most compilers stop with an error saying the type is redefined. The exact wording varies by compiler, but the cause is the same.
The Classic #ifndef Header Guard
The most widely used form relies on three preprocessor directives. #ifndef checks whether a macro is not yet defined, #define defines it, and #endif closes the block. Here is shapes.h with a guard added:
// shapes.h
#ifndef SHAPES_H
#define SHAPES_H
struct Point {
int x;
int y;
};
#endif // SHAPES_H
The first time the compiler reads this file, SHAPES_H is not defined, so the code inside the block is kept. The #define then sets the macro. If the same file is included again in that source file, SHAPES_H is already defined, so the compiler skips everything up to #endif.
The macro name is up to you, but it must match on all three lines. Put the guard around the entire header, not just part of it, because anything outside the guard is still read every time the header is included.
Using #pragma once
Many compilers, including GCC, Clang and MSVC, support #pragma once. You place it at the top of the header in place of the two guard lines:
// shapes.h
#pragma once
struct Point {
int x;
int y;
};
This does the same job in one line, and there is no macro name to choose. It is not part of the C++ standard, though, so check that your compiler supports it before relying on it in a project that must build in several environments. Copies of the same header stored in different folders may also be treated as different files. Some teams prefer the #ifndef form because it depends only on the macro name, so both forms are common in real code.
Choosing a Guard Name
The guard macro must be unique across the whole project. If two headers share a name, the compiler skips the second one without any message, and the errors appear later as missing types or functions. That makes the cause harder to spot.
A common style is to use uppercase letters, digits and underscores, and to build the name from the project and the file path. For example, net/socket.h in a project called myproject might use MYPROJECT_NET_SOCKET_H. Ending the name with _H makes its purpose clear.
Avoid names that begin with an underscore followed by a capital letter, or that contain two underscores in a row. The C++ standard reserves those patterns for the implementation, so a name like that can clash with a compiler’s own macros.
Common Mistakes
- Missing
#endif. Every#ifndefneeds a matching#endif. Without it, the compiler reports an error at the end of the file. - A guard that covers only part of the file. Anything outside the guard is still processed each time the header is included.
- Expecting the guard to work across source files. A guard works within one source file after preprocessing. Each
.cppfile that includes the header gets its own copy, and that is expected. - Defining ordinary functions in a header. Suppose a header contains
int square(int n) { return n * n; }and two.cppfiles include it. The linker then reports a multiple definition error, even with a guard. Mark the functioninlineso the same definition can appear in several source files, or move the body into a.cppfile. - Circular includes. If
a.hincludesb.handb.hincludesa.h, the guards stop the loop, but one header may be read before the types it needs are defined. A forward declaration, such asstruct Point;, often breaks the cycle when a header only needs pointers or references to that type.
Putting It Together
A typical header follows the same order:
- Put a guard, or
#pragma once, at the top. - Add the declarations: types, function prototypes, constants and templates.
- Close the guard with
#endifat the bottom, if you used#ifndef.
Keep each guard name unique, and mark any function defined in a header as inline. With those habits, the redefinition error from the start of this article should not come back.