2

I have a CMAKE file with the following compilation flags

 set (CMAKE_CXX_FLAGS_DEBUG  "${CMAKE_CXX_FLAGS_DEBUG} \
    -fPIC -Wall -pedantic -Wextra -Werror \
    -Wno-missing-braces -Wno-unused-variable \
    -Wno-ignored-qualifiers  -fdiagnostics-color")

I want to omit the -Wextra option for a single header file; /externals/include/foo.hpp (this is a third-party header-only library and gives error: [-Werror=unused-parameter] when compiled).

I have tried set_source_files_properties like this

set_source_files_properties(${EXTERNALS_SOURCE_DIR}/foo.hpp PROPERTIES COMPILE_FLAGS  "${CMAKE_CXX_FLAGS_DEBUG} -Wno-extra")

but couldn't get rid of the compilation error.

Is there a way to do that either in CMAKE or using #pragmas in the header file itself?

Thanks.

SOLUTION Here is how I got rid of the error:

  • Create a file foo_wrapper.hpp.
  • Add _pragma to ignore the trouble maker compilation flag
  • Use the wrapper header everywhere in the project instead of the actual header.

` // In file foo_wrapper.hpp:

   _Pragma("GCC diagnostic push")

   _Pragma("GCC diagnostic ignored \"-Wunused-parameter\"")

   #include "foo.hpp"

   _Pragma("GCC diagnostic pop")

`

starball
  • 20,030
  • 7
  • 43
  • 238
yabbasi
  • 43
  • 5
  • Is it possible that the unused parameter warning is turned on by another flag, maybe -Wall? – Raul Laasner Mar 03 '20 at 10:21
  • I checked by adding `-Wno-extra` to the main list of compile flags (mentioned in the post) and that compiles correctly. But I am required to keep this flag enabled for the rest of the project files and turn it off only for a single hpp. – yabbasi Mar 03 '20 at 15:37

1 Answers1

2

On current compilers, it is not possible to do this through build options.

This is because of how the build model works: The compiler will get invoked once for every source file and all the header files included by that source file will invariably use the same build options as the source file itself. So CMake will not be able to help you here.

Some compilers allow switching off certain warnings through #pragmas. For example, MSVC or gcc. Check your compiler's manual for what they offer in this regard. Unfortunately, this will always be non-portable, so if you have a code base supporting lots of compilers, the #pragmas can get lengthy. I would recommend writing a wrapper header that only includes the third party header giving you trouble and takes care of all the warning disabling. In your project you then always include the wrapper instead of the original third party header.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • Thank you! This solved my problem: - `I would recommend writing a wrapper header that only includes the third party header giving you trouble and takes care of all the warning disabling. In your project you then always include the wrapper instead of the original third party header.` – yabbasi Mar 03 '20 at 19:59