6

我正在使用CMake GUI(无版本)CMake3.6.1。我正在使用一个外部模块,add_subdirectory它向我显示了一些我不喜欢的警告(因为恼人的污染):

CMake Warning (dev) at D:/Sources/.../external/g3log/latest/Build.cmake:11 (IF):
  Policy CMP0054 is not set: Only interpret if() arguments as variables or
  keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  Quoted variables like "MSVC" will no longer be dereferenced when the policy
  is set to NEW.  Since the policy is not set the OLD behavior will be used.
Call Stack (most recent call first):
  D:/Sources/.../external/g3log/latest/CMakeLists.txt:72 (INCLUDE)
This warning is for project developers.  Use -Wno-dev to suppress it.

我想隐藏这些警告而不接触外部文件。-Wno-dev如果它只影响外部模块(g3log)就可以了。

我尝试使用cmake_policy如下没有效果:

cmake_policy(PUSH)
cmake_policy(SET CMP0054 OLD)
add_subdirectory(${g3log_DIR} ${CMAKE_BINARY_DIR}/../g3log)
cmake_policy(POP)
4

1 回答 1

4

将我的评论变成答案

听起来您的外部模块确实有project()命令。这将重置此子模块及以下的策略。

为了演示一个可能的解决方案,假设您有一个外部项目,如下所示:

g3log/CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(g3log NONE)

set(VAR1 "Hello World")
set(VAR2 "VAR1")
if ("${VAR2}" STREQUAL "${VAR1}")
    message("CMP0054 old behavior")
endif()

您现在可以设置CMAKE_POLICY_DEFAULT_CMP0054OLD(甚至更好NEW;没有人真正想要“旧”行为)以摆脱使用较新版本的 CMake 时会收到的“未设置策略 CMP0054”警告:

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(PolicyOverwrite NONE)

set(CMAKE_POLICY_DEFAULT_CMP0054 NEW)
add_subdirectory(g3log)

现在,如果在您的项目或您正在使用的外部项目之一中没有明确给出,则您已经为要使用的策略CMP0054设置了默认值。

于 2017-01-17T20:22:56.143 回答