首页 > 解决方案 > 使用 Qt 时删除 CMake 3.14.0 中的 CMP0020 错误

问题描述

我有以下CMakeFiles.txt

cmake_minimum_required (VERSION 3.14.0)

project (awfviewer)

message (STATUS "Building project ${PROJECT_NAME}")

set (Boost_USE_STATIC_LIBS ON)
set (Boost_USE_MULTITHREADED ON)
unset (Boost_INCLUDE_DIR CACHE)
unset (Boost_LIBRARY_DIRS CACHE)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
set (CMAKE_AUTOMOC ON)
set (CMAKE_AUTORCC ON)

find_package (Boost COMPONENTS program_options filesystem REQUIRED)
find_package (Qt5Core REQUIRED)
include_directories (${CMAKE_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR} ${Boost_INCLUDE_DIRS})

set (PROJECT_SRC
  main.cpp
  )

set (PROJECT_QRC
  ${CMAKE_CURRENT_SOURCE_DIR}/Resources/awfviewer.qrc
  )

add_executable (${PROJECT_NAME} ${PROJECT_SRC} ${PROJECT_QRC})

target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
qt5_use_modules (${PROJECT_NAME} Widgets Svg)

使用 CMake 3.14.0 构建我获得以下输出:

[cmake] The C compiler identification is MSVC 19.16.27030.1
[cmake] The CXX compiler identification is MSVC 19.16.27030.1
[cmake] Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
[cmake] Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe -- works
[cmake] Detecting C compiler ABI info
[cmake] Detecting C compiler ABI info - done
[cmake] Detecting C compile features
[cmake] Detecting C compile features - done
[cmake] Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
[cmake] Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe -- works
[cmake] Detecting CXX compiler ABI info
[cmake] Detecting CXX compiler ABI info - done
[cmake] Detecting CXX compile features
[cmake] Detecting CXX compile features - done
[cmake] Building project awfviewer
[cmake] Boost version: 1.69.0
[cmake] Found the following Boost libraries:
[cmake]   program_options
[cmake]   filesystem
[cmake] Boost version: 1.69.0
[cmake] Boost version: 1.69.0
[cmake] Building project connectiontest
[cmake] Looking for pthread.h
[cmake] Looking for pthread.h - not found
[cmake] Found Threads: TRUE  
[cmake] Boost version: 1.69.0
[cmake] Found the following Boost libraries:
[cmake]   unit_test_framework
[cmake]   log
[cmake]   system
[cmake]   date_time
[cmake]   log_setup
[cmake]   filesystem
[cmake]   thread
[cmake]   regex
[cmake]   chrono
[cmake]   atomic
[cmake] Building project awfconnectiontest
[cmake] Boost version: 1.69.0
[cmake] Found the following Boost libraries:
[cmake]   unit_test_framework
[cmake]   log
[cmake]   system
[cmake]   date_time
[cmake]   log_setup
[cmake]   filesystem
[cmake]   thread
[cmake]   regex
[cmake]   chrono
[cmake]   atomic
[cmake] Configuring done
[cmake] CMake Warning (dev) in src/AWFViewer/CMakeLists.txt:
[cmake]   Policy CMP0020 is not set: Automatically link Qt executables to qtmain
[cmake]   target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
[cmake]   Use the cmake_policy command to set the policy and suppress this warning.
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] CMake Warning (dev) in src/AWFViewer/CMakeLists.txt:
[cmake]   Policy CMP0020 is not set: Automatically link Qt executables to qtmain
[cmake]   target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
[cmake]   Use the cmake_policy command to set the policy and suppress this warning.
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] CMake Warning (dev) in thirdparty/WRibbon/src/WRibbon/CMakeLists.txt:
[cmake]   Policy CMP0020 is not set: Automatically link Qt executables to qtmain
[cmake]   target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
[cmake]   Use the cmake_policy command to set the policy and suppress this warning.
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] CMake Warning (dev) in src/AWFViewer/CMakeLists.txt:
[cmake]   Policy CMP0020 is not set: Automatically link Qt executables to qtmain
[cmake]   target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
[cmake]   Use the cmake_policy command to set the policy and suppress this warning.
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] CMake Warning (dev) in thirdparty/WRibbon/src/WRibbon/CMakeLists.txt:
[cmake]   Policy CMP0020 is not set: Automatically link Qt executables to qtmain
[cmake]   target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
[cmake]   Use the cmake_policy command to set the policy and suppress this warning.

/// repeat many times

[cmake] 
[cmake] Generating done
[cmake] CMake Warning:
[cmake]   Manually-specified variables were not used by the project:
[cmake] 
[cmake]     CMAKE_EXPORT_COMPILE_COMMANDS
[cmake] 
[cmake] 
[build] Starting build

/// Build starts here

我读过的输出cmake --help-policy CMP0020

Automatically link Qt executables to qtmain target on Windows.

CMake 2.8.10 and lower required users of Qt to always specify a link
dependency to the qtmain.lib static library manually on Windows.
CMake 2.8.11 gained the ability to evaluate generator expressions
while determining the link dependencies from IMPORTED targets.  This
allows CMake itself to automatically link executables which link to Qt
to the qtmain.lib library when using IMPORTED Qt targets.  For
applications already linking to qtmain.lib, this should have little
impact.  For applications which supply their own alternative WinMain
implementation and for applications which use the QAxServer library,
this automatic linking will need to be disabled as per the
documentation.

The OLD behavior for this policy is not to link executables to
qtmain.lib automatically when they link to the QtCore IMPORTED target.
The NEW behavior for this policy is to link executables to qtmain.lib
automatically when they link to QtCore IMPORTED target.

This policy was introduced in CMake version 2.8.11.  CMake version
3.14.0 warns when the policy is not set and uses OLD behavior.  Use
the cmake_policy command to set it to OLD or NEW explicitly.

.. note::
  The ``OLD`` behavior of a policy is
  ``deprecated by definition``
  and may be removed in a future version of CMake.

但我不明白我在 CMake 项目中使用 Qt 做错了什么。

我应该如何修改CMakelists.txt才能正确使用 Qt 并删除警告?

标签: c++qtcmake

解决方案


@ Jepessen,我试图复制您的问题,但遇到了同样的错误。在这种情况下,我通过使用 3.1 的旧版本绕过了这个问题CMake,并且我还指定了我想使用 C++ 语言来CMAKE_CXX_FLAGS使用特定的方向-std=c++11。您对您提到的文档是完全正确的。

CMake需要知道使用它的行为时,检查是否在项目中指定了它,如果没有,则将CMake行为设置OLD为默认值。因此,会弹出警告,要求用户注意这一点。

我通常尽量不包含cmake_policy(policy #),因为它们中的大多数是旧版本的,因此某些功能可能会被新版本弃用和替换。当然,可能存在cmake_policy(SET CMP<####> NEW)必须cmake_policy(SET CMP<####> OLD)特别解决的情况。

下面是工作代码(它在我的电脑上编译):

cmake_minimum_required (VERSION 3.1)
project (awfviewer)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
message (STATUS "Building project ${PROJECT_NAME}")

set (Boost_USE_STATIC_LIBS ON)
set (Boost_USE_MULTITHREADED ON)
unset (Boost_INCLUDE_DIR CACHE)
unset (Boost_LIBRARY_DIRS CACHE)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
set (CMAKE_AUTOMOC ON)
set (CMAKE_AUTORCC ON)

find_package (Boost COMPONENTS program_options filesystem REQUIRED)
find_package (Qt5Core REQUIRED)
include_directories (${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/main ${CMAKE_CURRENT_SOURCE_DIR} ${Boost_INCLUDE_DIRS})

set (PROJECT_SRC)
set (PROJECT_MAIN
  main.cpp
  )

add_executable (${PROJECT_NAME} ${PROJECT_SRC})
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})

在这种情况下,我唯一没有复制的是创建一个QRC文件,但对于这个小例子来说并不重要。通常CMake能够识别并接受新版本的CMake自身旧版本。

我在这种情况下创建的打印屏幕是这里

我希望这对您的项目有用


推荐阅读