GeneralPurpose-DuplicateIncludeGuardΒΆ

Different include files should not use the same include guard

Required inputs: IR

Every header file should have a unique include guard. Sometimes a header file is created as a copy of another header file and one forgets to update the include guard macro. This can result in problems when the same unit includes both header files.
Bad code (duplicate include guards):
// file: foo.h
#ifndef FOOBAR_H
#define FOOBAR_H
void foo();
#endif /* FOOBAR_H */

// file: bar.h
#ifndef FOOBAR_H  // ERROR: Duplicate include guard.
#define FOOBAR_H
void bar();
#endif /* FOOBAR_H */

// file: main.c
#include "foo.h"
#include "bar.h"  // ERROR: the contents of bar.h will be ignored
Good code (unique include guards):
// file: foo.h
#ifndef FOO_H
#define FOO_H
void foo();
#endif /* FOO_H */

// file: bar.h
#ifndef BAR_H  // OK: different include guard than in foo.h
#define BAR_H
void bar();
#endif /* BAR_H */

// file: main.c
#include "foo.h"
#include "bar.h"  // OK: the contents of bar.h will be included

Possible Messages

Key

Text

Severity

Disabled

duplicate_include_guard

Duplicate include guard.

None

False

Options