Anton
Anton

Reputation: 1538

C++ compiler flag to ignore warnings for external libraries but without including directory

There are two ways, as far as I can tell, to ignore warnings for external libraries.

First way is to use #pragma:

#pragma gcc diagnostic ignored "-Wunused-parameter"

Second way is to add -isystem/path/to/system/lib to the compiler flags. This labels that particular include path as an external include path, and so the compiler will not emit warnings stemming from those includes.

I have a large project with many compilation targets, some of which use particular third party libraries which cause problems for my compiler. For reasons I cannot use the #pragma option.

But if I use the -isystem as a blanket compiler flag for all build targets, then I will unfortunately include /path/to/system/lib for every other build target, even those which do not use that system library. This means other targets will search through those external libraries, which is not desirable.

Is there a compiler option, similar to -isystem which can be added in a blanket way, which does not add to the search path, but only excludes warnings if the path happens to already be included in the search path?

Upvotes: 3

Views: 2094

Answers (1)

Florian Weimer
Florian Weimer

Reputation: 33727

I have a large project with many compilation targets, some of which use particular third party libraries which cause problems for my compiler. For reasons I cannot use the #pragma option.

Can you elaborate on this? Why is a wrapper header with #pragma GCC diagnostic not an option? That is, something like this:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include </path/to/real/include.h>
#pragma GCC diagnostic pop

(Or if you want to get fancy, use #include_next.) You would apply this only when crossing subsystem boundaries; the subsystem itself would be compiled with -Wunused-parameter.

I don't see anything in the GCC/libcpp sources which would allow one to reset the system header flag without adding a new system header. In any case, this looks to me like something which could be reasonably addressed within the build system.

Upvotes: 2

Related Questions