Given the following code:
#if MACRO_WITHOUT_A_VALUE
int var;
#endif
int main(){}
When compiled with, g++ -std=c++1z -Wundef -o main main.cpp
,
it produces the following warning:
main.cpp:1:5: warning: "MACRO_WITHOUT_A_VALUE" is not defined [-Wundef]
#if MACRO_WITHOUT_A_VALUE
^
I'd like to keep the warning flag enabled, but suppress this particular instance.
I apply the following:
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wundef"
#pragma GCC diagnostic push
#endif
#if MACRO_WITHOUT_A_VALUE
int var;
#endif
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
int main(){}
This only solves the problem in clang++
.
The command clang++ -std=c++1z -Wundef -o main main.cpp
builds without warnings.
The command g++ -std=c++1z -Wundef -o main main.cpp
builds with the same [-Wundef]
warning as before.
How can I suppress -Wundef
warnings in g++
?
g++ (Ubuntu 5.1.0-0ubuntu11~14.04.1) 5.1.0
clang version 3.8.0
What I've done before when third party headers were inducing warnings was to wrap them in my own private header that uses #pragma GCC system_header
to just silence all the warnings from that header. I use my own wrapper to keep the includes neat and allow for an additional customization point in the future if needed.
This isn't disabling the warning, but fixing the preprocessor code to avoid it.
The below tests are based on a similar issue here, using clang -Weverything
...
#define ZERO 0
#define ONE 1
#define EMPTY
// warning: 'NOTDEFINED' is not defined, evaluates to 0 [-Wundef]
#if NOTDEFINED
#warning NOTDEFINED
#endif
// false
#if ZERO
#warning ZERO
#endif
// true
#if ONE
#warning ONE
#endif
// error: expected value in expression
#if EMPTY
#warning EMPTY
#endif
// false
#if defined(NOTDEFINED) && NOTDEFINED
#warning NOTDEFINED
#endif
// false
#if defined(ZERO) && ZERO
#warning ZERO
#endif
// true
#if defined(ONE) && ONE
#warning ONE
#endif
// error: expected value in expression
#if defined(EMPTY) && EMPTY
#warning EMPTY
#endif
The one liner #if defined(SOME_MACRO) && SOME_MACRO
can avoid this warning. To explicitly handle the case...
#if defined(DEBUG_PRINT)
#if DEBUG_PRINT
... true
#else
... false
#endif
#else
#error DEBUG_PRINT must be defined
#endif
To handle EMPTY
see this: How to test if preprocessor symbol is #define'd but has no value?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With