Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove CMP0020 error in CMake 3.14.0 when using Qt

Tags:

c++

cmake

qt

I've the following 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)

Building with CMake 3.14.0 I obtain following output:

[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

I've read the output of 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.

But I don't understand what I'm doing wrong by using Qt in the CMake project.

How should I modify the CMakelists.txt in order to use Qt properly and remove the warning?

like image 635
Jepessen Avatar asked Nov 26 '25 00:11

Jepessen


1 Answers

@ Jepessen, I tried to replicate your problem and I got your same error. I bypassed the problem by using an older version of CMake, 3.1 in this case, and also I specified that I wanted to use C++ language using CMAKE_CXX_FLAGS with specific directions -std=c++11. You are totally correct about the documentation you mentioned.

When CMake needs to know the behavior to use it checks if this is specified within the project, and if it is not, then CMake sets the OLD behavior as default. As a consequence of that the warning pops up asking the user to take care of that.

I usually try not to include cmake_policy(policy #) as the majority of them are for older versions and consequently some functionalities could be deprecated and substituted by new versions. Of course there could be situations where cmake_policy(SET CMP<####> NEW) and cmake_policy(SET CMP<####> OLD) have to be specifically addressed.

Below is the working code (it compiles on my computer):

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})

In this case the only thing I didn't replicate was creating a QRC file but it does not matter for the small example. Usually CMake is able to recognize and accept older versions of CMake itself given the newer version.

The print screen of what I created in this case is here

I hope this could be useful for your project

like image 58
Emanuele Avatar answered Nov 27 '25 14:11

Emanuele



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!