Fundamentals 7 min read

How to Prevent Duplicate Header Inclusion in C/C++: #ifndef vs #pragma once

This guide explains why including the same header file multiple times causes redefinition errors in C/C++ and demonstrates two common techniques—using #ifndef guards and the #pragma once directive—to ensure each header is processed only once.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Prevent Duplicate Header Inclusion in C/C++: #ifndef vs #pragma once

Problem with repeated header inclusion

When a source file includes the same header multiple times, the preprocessor copies the header contents each time, causing duplicate definitions of classes, functions, or variables and violating the One Definition Rule. Example:

// math.h
class Math {
public:
    int add(int a, int b);
    int subtract(int a, int b);
};
// main.cpp
#include "math.h"
#include "math.h" // duplicate include
int main() {
    Math m;
    return 0;
}
Result: error: redefinition of 'class Math'

Header guard using #ifndef

Wrap the header content with a unique macro.

// math.h
#ifndef MATH_H
#define MATH_H

class Math {
public:
    int add(int a, int b);
    int subtract(int a, int b);
};

#endif // MATH_H

The macro is defined on the first inclusion; subsequent includes skip the body, preventing duplicate definitions.

Header guard using #pragma once

Place a single directive at the top of the header.

// math.h
#pragma once

class Math {
public:
    int add(int a, int b);
    int subtract(int a, int b);
};

The compiler ensures the file is processed only once.

Comparison

#ifndef : Standard C/C++ preprocessor directive, supported by all compilers. Requires three lines and a unique macro name.

#pragma once : Concise one‑line directive, typically faster because the compiler handles inclusion directly, and avoids macro name collisions. May not be supported by very old compilers.

Both techniques effectively prevent duplicate header inclusion; choose the one that best fits your project's portability and compiler support requirements.

图片
图片
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

C++#pragma onceduplicate definitionheader guards#ifndef
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.