在 CMake CHECK_CXX_COMPILER_FLAG 中检查了两次编译器标志
Posted
技术标签:
【中文标题】在 CMake CHECK_CXX_COMPILER_FLAG 中检查了两次编译器标志【英文标题】:Compiler flag checked twice in CMake CHECK_CXX_COMPILER_FLAG 【发布时间】:2017-07-22 00:13:54 【问题描述】:我使用自定义宏来检查并向 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://***.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 应该找到这个并且不要再次检查它(使用 MSVC 的 try_compile 非常慢)
它适用于大多数标志,但不适用于-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 运行中未设置,为什么只有这个?
【问题讨论】:
【参考方案1】:您将 CMake 与您的 =
登录变量名混淆了吗?
试试这个:
STRING(REGEX REPLACE "[-=]" "_" VarName $VarName)
【讨论】:
以上是关于在 CMake CHECK_CXX_COMPILER_FLAG 中检查了两次编译器标志的主要内容,如果未能解决你的问题,请参考以下文章
如何在Windows开发环境中用CMake改变CMAKE_INSTALL_PREFIX的值?