我使用自定义宏检查并在CMAKE中为编译添加编译器标志。我用于此的代码是:
MACRO(CheckAndAddFlag flag)
# We cannot check for -Wno-foo as this won't throw a warning so we must check for the -Wfoo option directly
# http://stackoverflow.com/questions/38785168/cc1plus-unrecognized-command-line-option-warning-on-any-other-warning
STRING(REGEX REPLACE "^-Wno-" "-W" checkedFlag ${flag})
SET(VarName ${checkedFlag})
STRING(REPLACE "+" "X" VarName ${VarName})
STRING(REPLACE "-" "_" VarName ${VarName})
# Avoid double checks. A compiler will not magically support a flag it did not before
MESSAGE(STATUS "Checking CXX_FLAG_${VarName}_SUPPORTED. Checked? ${VarName}_CHECKED= ${${VarName}_CHECKED} end")
if(NOT ${VarName}_CHECKED)
CHECK_CXX_COMPILER_FLAG(${checkedFlag} CXX_FLAG_${VarName}_SUPPORTED)
CHECK_C_COMPILER_FLAG(${checkedFlag} C_FLAG_${VarName}_SUPPORTED)
set(${VarName}_CHECKED YES CACHE INTERNAL "")
MESSAGE(STATUS "Checked CXX_FLAG_${VarName}_SUPPORTED. Checked? ${VarName}_CHECKED= ${${VarName}_CHECKED} end")
endif()
IF (CXX_FLAG_${VarName}_SUPPORTED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
ENDIF ()
IF (C_FLAG_${VarName}_SUPPORTED)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
ENDIF ()
unset(VarName)
unset(checkedFlag)
ENDMACRO()
我称它为用:
CheckAndAddFlag("-Wno-error=type-limits")
CheckAndAddFlag("-Wfloat-conversion")
通常它应该只检查每个标志一次,设置它的变量,然后运行CMake应该找到它并且不要再检查它(try_compile与MSVC的速度很慢)
它适用于大多数标志,但它不适用于-Wno-error=type-limits
。这里根据输出对标志进行双重检查。因此我添加了_CHECKED
变量来解决这个问题,但即使这样也行不通。它会被再次检查。
一些输出:
Checking CXX_FLAG__Werror=type_limits_SUPPORTED. Checked? _Werror=type_limits_CHECKED= end
Performing Test CXX_FLAG__Werror=type_limits_SUPPORTED
Performing Test CXX_FLAG__Werror=type_limits_SUPPORTED - Failed
Performing Test C_FLAG__Werror=type_limits_SUPPORTED
Performing Test C_FLAG__Werror=type_limits_SUPPORTED - Failed
Checked CXX_FLAG__Werror=type_limits_SUPPORTED. Checked? _Werror=type_limits_CHECKED= YES end
Checking CXX_FLAG__Wfloat_conversion_SUPPORTED. Checked? _Wfloat_conversion_CHECKED= YES end
Checking CXX_FLAG__Wlong_long_SUPPORTED. Checked? _Wlong_long_CHECKED= YES end
为什么会发生这种情况?为什么变量设置但在下一次CMAKE运行中未设置,为什么只有这个?
答案 0 :(得分:1)
您的CMake与变量名称=
签名混淆了吗?
请改为尝试:
STRING(REGEX REPLACE "[-=]" "_" VarName ${VarName})