Trevor Hickey Trevor Hickey - 3 months ago 19
C++ Question

-Wundef is not being ignored with pragma in g++

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

Answer

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.