Устранить ошибку CMP0020 в CMake 3.14.0 при использовании Qt - PullRequest
1 голос
/ 18 апреля 2019

У меня есть следующее 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.

Но я не понимаю, что я делаю неправильно, используя Qt в проекте CMake.

Как мне изменить CMakelists.txt, чтобы правильно использовать Qt и удалить предупреждение?

1 Ответ

0 голосов
/ 23 апреля 2019

@ Jepessen, я пытался повторить твою проблему, и я получил твою ту же ошибку.Я обошел проблему, используя в этом случае более старую версию CMake, 3.1, а также указал, что хочу использовать язык 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, учитывая более новую версию.

Экран печати того, что я создал в этом случае, here

Я надеюсьэто может быть полезно для вашего проекта

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...