1

I have a c library to maintain, which is built by cmake. When I start to build it, it tells me that

cc1: warning: unrecognized command line option "-Wno-incompatible-pointer-types" [enabled by default]

yes, my gcc version is 4.8.5 and does not have that flag. So I want to disable it when the compiler does not support that flag.

After some searching, I came to this solution

include(CheckCCompilerFlag)
check_c_compiler_flag(-Wno-invalid-offsetof HAS_NO_INVALID_OFFSETOF)
if (HAS_NO_INVALID_OFFSETOF)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-invalid-offsetof")
endif()

but when I add it to my CMakeLists.txt, it does not work, the above warning still comes out.

After searching the cmake documentation, it tells me that

A positive result from this check indicates only that the compiler did not issue a diagnostic message when given the flag. Whether the flag has any effect or even a specific one is beyond the scope of this module.

According to that, my gcc does not support that flag, it won't issue a diagnostic message, so cmake thinks that my gcc supports that flag?

Is there any way to make it act as the way I like it to do ?

Alex
  • 1,737
  • 2
  • 20
  • 35

1 Answers1

0

Quoting from man gcc

When an unrecognized warning option is requested (e.g., -Wunknown-warning), GCC emits a diagnostic stating that the option is not recognized. However, if the -Wno- form is used, the behavior is slightly different: no diagnostic is produced for -Wno-unknown-warning unless other diagnostics are being produced. This allows the use of new -Wno- options with old compilers, but if something goes wrong, the compiler warns that an unrecognized option is present.

You could modify your check to the following

include(CheckCCompilerFlag)
check_c_compiler_flag(-Winvalid-offsetof HAS_INVALID_OFFSETOF)
if (HAS_INVALID_OFFSETOF)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-invalid-offsetof")
endif()

This could theoretially break for some non-gcc compiler which happens to support -Winvalid-offsetof but not -Wno-invalid-offsetof. So if you want to be extra safe, add another check.

chtz
  • 17,329
  • 4
  • 26
  • 56