代码之家  ›  专栏  ›  技术社区  ›  Jepessen

使用Qt时删除CMake 3.14.0中的CMP0020错误

  •  0
  • Jepessen  · 技术社区  · 7 年前

    我有以下几点 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并消除警告?

    0 回复  |  直到 7 年前
        1
  •  1
  •   Emanuele    7 年前

    @杰佩森,我试图复制你的问题,但我得到了你同样的错误。我通过使用旧版本的 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

    我希望这对你的项目有用

    推荐文章