try_compile with /WX or -Werror


When defining global CMAKE_C_FLAGS/CMAKE_CXX_FLAGS variables for a platform, most of the time we’ll set /WX and -Werror, but also enable extra warnings (or MSVC also has some undocumented ones for C++/CX). I just realized that it makes some checks (like detecting functions or symbols) fail just because they emitted a warning. For example, as we enable deprecated warnings on MSVC, some functions might be detected as unavailable because they are deprecated. Or, when compiling with C++/CX enabled, as all checks are using a regular main function instead of one with the threading model, we get the undocumented C4447 warning and all checks fail.

I guess I could try to set CMAKE_REQUIRED_FLAGS and try to remove the warning-as-errors flag, but at the same time I’m wondering if there’s a better way to do it when you expect other teams to use your project and provide their global compilation flags through CMAKE_C_FLAGS/CMAKE_CXX_FLAGS.

You can try and add to CMAKE_C{,XX}_FLAGS after the try_compile checks run. I believe compiler-wide flags are looked up in-scope at the end of the top-level CMakeLists.txt file if you want to defer them to that point. Alternatively, you can add them as target_compile_options() to a build-local target and link to it from all your targets (with $<BUILD_INTERFACE:> on the flag or the link to the target. The former makes it easier to link to your “flag target” whereas the other makes it so you don’t have to export the target to an export set.