Disable # warning for only one header

In C / C ++, the code I'm trying to install includes an outdated system header:

From the title:

#ifdef __GNUC__ #warning "this header is deprecated" #endif 

As we compile here with gcc -Wall -Werror , compilation stops. Ultimately, itโ€™s better to replace obsolete features, but for now I want to disable this warning.

Compiling without -Werror certainly works, but since it is part of a fully automated build process, I prefer not to.

Enabling the header with #undef ing __GNUC__ before and #define then possible, but I'm worried about side effects inside the included header.

Is there a way to disable #warning or relax -Werror for just one header?

+7
c gcc c-preprocessor preprocessor-directive
source share
2 answers

You can do this with a specific (GCC) diagnostic pragma

If you surround include with the following, it will disable any warnings caused by #warning .

 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcpp" #include "header.h" #pragma GCC diagnostic pop 

Note that if you change ignored to warning in the above, the compiler still prints warnings - it just doesn't act on the -Werror flag for them.

+8
source share

This disables exactly one type of warning, the #warning directive, so I believe this is a safe solution to this problem:

 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-W#warnings" #include <evilheader.h> #pragma GCC diagnostic pop 

( Edit : Sorry, it turns out that gcc is actually calling on my system, so it may not work with your genuine gcc)

+2
source share

All Articles