Use -Wmacro-redefined
flag to warn redefined macros by clang.
#define MY_DEFINE 111
#define MY_DEFINE 222
int main(void)
{
return 0;
}
$ # M1 Mac (Ventura) + Apple clang 14.0.0
$ clang sample.cpp -Wmacro-redefined
sample.cpp:2:9: warning: 'MY_DEFINE' macro redefined [-Wmacro-redefined]
#define MY_DEFINE 222
^
sample.cpp:1:9: note: previous definition is here
#define MY_DEFINE 111
^
1 warning generated.
The warnings are displayed automatically: clang enables -Wmacro-redefined
by default.
$ # M1 Mac (Ventura) + Apple clang 14.0.0
$ clang sample.cpp
sample.cpp:2:9: warning: 'MY_DEFINE' macro redefined [-Wmacro-redefined]
#define MY_DEFINE 222
^
sample.cpp:1:9: note: previous definition is here
#define MY_DEFINE 111
^
1 warning generated.
Suppress warnings
Use -Wno-macro-redefined
to suppress warnings.
$ # M1 Mac (Ventura) + Apple clang 14.0.0
$ clang sample.cpp -Wno-macro-redefined
$
GCC
GCC also accepts -Wmacro-redefined
. On macOS, gcc
is an alias of clang: we need to use gcc-N
(N: the version of gcc) instead.
$ # M1 Mac (Ventura) + gcc 12.2.0
$ gcc-12 sample.cpp
$sample.cpp:2: warning: "MY_DEFINE" redefined
2 | #define MY_DEFINE 222
|
sample.cpp:1: note: this is the location of the previous definition
1 | #define MY_DEFINE 111
|
References
- Clang documentation — DIAGNOSTIC FLAGS IN CLANG
https://clang.llvm.org/docs/DiagnosticsReference.html#wmacro-redefined