Include guards and #pragma once
An include guard stops a header's contents from being pasted into the same translation unit more than once. The classic form is an #ifndef/#define/#endif wrapper; #pragma once is the shorter non-standard equivalent.
Detailed Explanation
The preprocessor is a text substitution tool. When it meets #include "Shape.hpp" it replaces that line with the entire contents of the file. It does not remember that it already did so a moment ago.
Now picture three headers. Shape.hpp defines class Shape. Circle.hpp includes Shape.hpp because Circle derives from it. Renderer.hpp includes both. After preprocessing, Renderer.cpp contains the text of class Shape twice, and the compiler reports a redefinition error. This is the diamond of includes, and in any project past a few files it is unavoidable — you cannot solve it by carefully ordering includes at the call site.
The guard makes the second inclusion produce nothing. You wrap the whole header in #ifndef CODEPHATAA_SHAPE_HPP, immediately #define that same macro, and close with #endif at the bottom. The first time through, the macro is not defined, so the preprocessor defines it and keeps the body. The second time, the macro exists, so everything up to #endif is skipped. The technique is older than C++ and has been standard practice in C since the 1970s.
#pragma once does the same thing in one line at the top of the file. The compiler tracks which files it has already included and skips repeats. It is not in the C++ standard — #pragma is by definition the escape hatch for implementation-specific directives — but GCC, Clang, MSVC, ICC, and every other compiler you are likely to meet support it. In practice it is portable.
Three reasons teams pick it. It is one line instead of three plus a name. It cannot collide with another header's macro. And it is usually faster: the compiler recognises the file by identity and skips reopening it, whereas a traditional guard requires opening and lexing the whole file again to discover it will be discarded. Modern compilers detect the traditional pattern and apply the same optimisation, so the speed gap is smaller than it once was.
Where #pragma once breaks is file identity. The compiler decides "same file" using the path, inode, size, and timestamp, depending on the implementation. If the same header is reachable through a symlink and through its real path, or is physically copied into two include directories, or is hard-linked by a vendoring script, the compiler may treat the copies as different files and include both. A traditional guard is immune, because it keys on the macro name rather than the file. Network drives and some container mount setups have historically confused inode checks too.
Traditional guards have the opposite failure. The macro name is a global identifier in a flat namespace shared with every header in your project and every library you consume. Two files both using #define UTILS_H means the second one silently vanishes, and the error you get is a missing class hundreds of lines later. Name guards after the full path with a project prefix — CODEPHATAA_NET_SOCKET_HPP — and never _UTILS_H or __UTILS_H__, because identifiers with a leading underscore followed by a capital, or containing a double underscore, are reserved for the implementation.
A guard prevents double inclusion within one translation unit. It does nothing about the one-definition rule across translation units — a non-inline function defined in a guarded header still produces a duplicate symbol at link time.
Code Examples
net/Socket.hpp — traditional guard, named after the path with a project prefix.
// net/Socket.hpp — traditional guard, named after the path with a project prefix.
#ifndef CODEPHATAA_NET_SOCKET_HPP
#define CODEPHATAA_NET_SOCKET_HPP
#include <string>
class Socket {
public:
explicit Socket(std::string host, int port);
bool connect();
private:
std::string host_;
int port_;
};
#endif // CODEPHATAA_NET_SOCKET_HPPnet/Socket.hpp — the same file with the modern one-liner.
// net/Socket.hpp — the same file with the modern one-liner.
#pragma once
#include <string>
class Socket {
public:
explicit Socket(std::string host, int port);
bool connect();
private:
std::string host_;
int port_;
};Why it is needed at all. Without a guard in Shape.hpp:
// Why it is needed at all. Without a guard in Shape.hpp:
#include "Shape.hpp" // pastes: class Shape { ... };
#include "Circle.hpp" // Circle.hpp itself includes Shape.hpp
// -> pastes class Shape a second time
// error: redefinition of 'class Shape'Common Gotchas
A stray `#endif` or a typo in the macro name silently disables the guard
`#ifndef FOO_HPP` paired with `#define FOO_HP` means the body is included every time. The resulting redefinition error points at the class, not at the guard.
`#pragma once` can misfire on duplicated or symlinked headers
if a build system copies a vendored header into two include paths, the compiler may treat them as distinct files and include both. Some projects use both mechanisms together for this reason.
Guards do not satisfy the one-definition rule across files
they protect one translation unit. A non-`inline` function body in a guarded header still gives a duplicate symbol error when two source files include it.
Do not put a guard around an `#include` at the call site
wrapping `#include "Shape.hpp"` in your own `#ifdef` is a workaround for a missing guard in the header itself. Fix the header.