In a not-so-tiny project (>100 .h
), I want to make sure that every .h
use different include-guards (#ifndef XXX
).
Whenever I violate it, I want to be informed as soon as possible.
Situation
Here is the convention I am using (semi-auto-generated, VS2015):-
Foo.h
#ifndef Foo_h__
#define Foo_h__
//....... some long code .........
#endif //Foo_h__
Sometimes (once per 1-4 weeks), after I created new files, copy-and-paste sources or refactor(rename) extensively, I make some mistakes about the include guard. As a result :-
- Include guard does not match with file name anymore.
- Include guard happens to be same as another include guard in another header.
Question
How to prevent and cure duplication of include guards?
My poor solution
Solution 1:-
#pragma once
Disadvantage: It is compiler specific. I don't want my code to be compiler-dependent.
Solution 2:-
(Edited, thank Benjamin Lindley and Beta.)
I may code an auto-code-generation script. This is a possible standard:-
<project>_<path_part1>_..._<path_partN>_<file>_<extension>_INCLUDED
Disadvantage: It required an external script.