-
Notifications
You must be signed in to change notification settings - Fork 0
Include Guards vs #pragma once
MarekBykowski edited this page May 26, 2026
·
1 revision
// Traditional include guard — portable, standard C
#ifndef MY_HEADER_H
#define MY_HEADER_H
// ... header content ...
#endif // MY_HEADER_H
// #pragma once — simpler, supported by GCC/Clang/MSVC, not standard C
#pragma once
// ... header content ...| Include guard | #pragma once |
|
|---|---|---|
| Standard C | ✓ | ✗ (compiler extension) |
| GCC / Clang / MSVC | ✓ | ✓ |
| Handles symlinks/copies | ✓ | ✗ (may include twice) |
| Less boilerplate | ✗ | ✓ |
| Linux kernel uses | ✓ guards | — |
💡 For embedded/cross-compiler work: use include guards.
#pragma onceis fine for most projects but fails edge cases with symlinked headers.