3

My project uses -Werror to turn GCC 11.3.0 g++ warnings to errors and stop the build. But a 3rd party header file we need to include causes a bunch of warnings to be issued by the compiler.

I was hoping there is a way to turn off -Werror for that 1 include file. Maybe a #pragma I can issue just before and just after that header file is invoked?

However, it does not seem to be the case. Best I can find is to disable individual warnings with #pragma GCC diagnostic ..., which would make me list all of the warnings that this header file causes.

Is that really the case or did I miss something obvious?


Edit:

Turns out some warnings/errors can be disabled, but others cannot. For example, this works to remove the signed/unsigned warnings:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <problematic_include.hpp>
#pragma GCC diagnostic pop

But this does not work:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#include <problematic_include.hpp>
#pragma GCC diagnostic pop

...to remove the error about error: ignoring ‘#pragma _CVUI_COMPILE_MESSAGE ’ [-Werror=unknown-pragmas].

Stéphane
  • 19,459
  • 24
  • 95
  • 136
  • See also: https://stackoverflow.com/q/1867065/5754656 (Use `include_directories(SYSTEM "/path/to/cvui")` in CMake or otherwise `-isystem` instead of `-I`) – Artyer Apr 12 '23 at 23:01

1 Answers1

1

I tried to not name the 3rd party library that caused this, but it turns out in this case the library itself provides a solution to the problem.

The library is CVUI, which provides some simple GUI functionality based on OpenCV. It is a header-only library. Turns out it has a macro called CVUI_DISABLE_COMPILATION_NOTICES which prevents the problematic #pragma line that was causing g++ to stop compiling.

In the end, as ugly as this is, here is the solution I chose:

#define CVUI_IMPLEMENTATION
#define CVUI_DISABLE_COMPILATION_NOTICES
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "cvui.h"
#pragma GCC diagnostic pop

This way the cvui.h file can be successfully included from within a project that uses -Wall -Wextra -Werror.

Stéphane
  • 19,459
  • 24
  • 95
  • 136
  • you can create a `cvui_wnoerror.h` that contains this ugly boilerplate and in your project you can include this header instead of doing all of this everywhere you include `cvui.h` – bolov Apr 10 '23 at 01:18