将QT5库添加到CMake [英] Adding QT5 libraries to CMake

查看:109
本文介绍了将QT5库添加到CMake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用CMake。我正在尝试创建一个简单的CMakeList文件,并从QT5 5.7添加对QPrinter和QTextDocument的支持。从我发现不适的地方,必须将以下库添加到我的CMakeList文件中:

I'm new to using CMake. I'm trying to create a simple CMakeList file and add support for QPrinter and QTextDocument from QT5 5.7. From what I've found ill have to add the following libraries to my CMakeList file:


  • QT5Core

  • QT5PrintSupport

  • Qt5Gui

  • Qt5Widgets

  • QT5Core
  • QT5PrintSupport
  • Qt5Gui
  • Qt5Widgets

这就是我现在拥有的东西:

This is what i have right now:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR) 
PROJECT (photobooth)

find_package( Qt5Core )
find_package( Qt5PrintSupport )
find_package( Qt5Gui )
find_package( Qt5Widgets )

set( NAME_SRC
    src/main.cpp
    src/photobooth.cpp      
)

set( NAME_HEADERS 
    include/photobooth.h          

)

INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)

add_executable( photobooth ${NAME_SRC} ${NAME_HEADERS} )

target_link_libraries( photobooth Qt5::Widgets )
target_link_libraries( photobooth Qt5::Core )
target_link_libraries( photobooth Qt5::Qt5PrintSupport )
target_link_libraries( photobooth Qt5::Qt5Gui )

这是Cmake的输出:

This is the output from Cmake:

Configuring done
CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0020 is not set: Automatically link Qt executables to qtmain
  target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0020 is not set: Automatically link Qt executables to qtmain
  target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:24 (add_executable):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "photobooth" links to target "Qt5::Qt5PrintSupport" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:24 (add_executable):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "photobooth" links to target "Qt5::Qt5Gui" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0020 is not set: Automatically link Qt executables to qtmain
  target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:24 (add_executable):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "photobooth" links to target "Qt5::Qt5PrintSupport" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:24 (add_executable):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "photobooth" links to target "Qt5::Qt5Gui" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0020 is not set: Automatically link Qt executables to qtmain
  target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:24 (add_executable):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "photobooth" links to target "Qt5::Qt5PrintSupport" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:24 (add_executable):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "photobooth" links to target "Qt5::Qt5Gui" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0020 is not set: Automatically link Qt executables to qtmain
  target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:24 (add_executable):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "photobooth" links to target "Qt5::Qt5PrintSupport" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:24 (add_executable):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "photobooth" links to target "Qt5::Qt5Gui" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0020 is not set: Automatically link Qt executables to qtmain
  target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0020 is not set: Automatically link Qt executables to qtmain
  target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0020 is not set: Automatically link Qt executables to qtmain
  target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0020 is not set: Automatically link Qt executables to qtmain
  target on Windows.  Run "cmake --help-policy CMP0020" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:24 (add_executable):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "photobooth" links to target "Qt5::Qt5PrintSupport" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:24 (add_executable):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "photobooth" links to target "Qt5::Qt5Gui" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

Generating done

我的项目使用Visual Studio 14 2015 Win64生成的效果很好我的构建。另外,当我在main.cpp中包含QTextDocument时,它会找到它,但不会找到QPrinter。我有做错什么吗?

My project generates fine with Visual Studio 14 2015 Win64 as my build. Also when i include QTextDocument in main.cpp it finds it but not QPrinter. Is there anything I'm doing incorrectly?

推荐答案

一些提示:

find_package( Qt5Core )
find_package( Qt5PrintSupport )
find_package( Qt5Gui )
find_package( Qt5Widgets )

您可以将这些调用合并到一个 find_package 中,以查找Qt5:

You can merge these calls in one find_package looking for Qt5:

find_package(Qt5 REQUIRED
    Core
    PrintSupport
    Gui
    Widgets
)

请注意使用关键字 REQUIRED 会导致CMake如果找不到包,则会引发错误。查看您的错误日志,这可能是您的问题。您是否在某处设置了 Qt5_DIR

Note the use of the keyword REQUIRED which will cause CMake to raise an error if the package can't be found. Looking in your error log, that may be your issue. Did you set Qt5_DIR somewhere?

Core Gui 在这里是可选的,因为小部件取决于它们,因此它们将被自动添加。

Core and Gui are optional here since Widgets depends on them, so they will be added automatically.

您应将CMake的所需版本更改为 2.8.11 ,以确保导入的目标嵌入目录。

You should change your required version for CMake to 2.8.11 to ensure imported targets embed include directories.

您应该考虑使用 target_include_directories 而不是 INCLUDE_DIRECTORIES 来避免全球污染,并将参数附加到目标上。这不是真正的意义,但对于将来的大型项目来说是个好习惯。

You should consider target_include_directories instead of INCLUDE_DIRECTORIES to avoid global pollution, and attach the parameter to the target. This is not a real point here, but a good practice for future bigger projects.

您对 link_directories 的调用是这里没用。如果需要链接到库,则无论如何都更喜欢 target_link_libraries

Your call to link_directories is useless here. If you need to link against libraries, prefer target_link_libraries anyway.

EXECUTABLE_OUTPUT_PATH 是一个旧变量,由<$ c $取代c> RUNTIME_OUTPUT_DIRECTORY 是目标属性。

清理后,您的CMake项目可能看起来像:

Once cleaned, your CMake project may looks like:

cmake_minimum_required(VERSION 2.8.11 FATAL_ERROR)
PROJECT (photobooth)

find_package(Qt5 REQUIRED
    Widgets
    PrintSupport
)

set( NAME_SRC
    src/main.cpp
    src/photobooth.cpp
)
set( NAME_HEADERS
    include/photobooth.h
)

add_executable( photobooth ${NAME_SRC} ${NAME_HEADERS} )

set_target_properties(photobooth PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

target_include_directories(photobooth PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

target_link_libraries( photobooth Qt5::Widgets Qt5::PrintSupport )

这篇关于将QT5库添加到CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆