将QT5库添加到CMake

7
我是一名新手,正在使用CMake。我正在尝试创建一个简单的CMakeList文件,并为来自QT5 5.7的QPrinter和QTextDocument添加支持。从我发现的情况来看,我需要将以下库添加到我的CMakeList文件中:
  • QT5Core
  • QT5PrintSupport
  • Qt5Gui
  • Qt5Widgets
这是我目前的文件内容:
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的输出内容:

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。是否有什么我做错了的地方?


2
也许您忘记了在qmake的include目录中添加内容,例如 target_include_directories(photobooth ${Qt5Widgets_INCLUDE_DIRS})。您看过这个吗?顺便提一下,尝试升级您的cmake版本。 - Stargateur
2个回答

12

一些提示:

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

您可以在一个 find_package 中合并这些调用,以寻找Qt5:

find_package(Qt5 REQUIRED
    Core
    PrintSupport
    Gui
    Widgets
)

注意使用关键字REQUIRED,如果找不到该软件包,CMake将会报错。根据你的错误日志看起来这可能是你的问题所在。你是否在某个地方设置了Qt5_DIR

这里CoreGui是可选的,因为Widgets依赖于它们,所以它们将被自动添加。

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

您应该考虑使用target_include_directories代替INCLUDE_DIRECTORIES以避免全局污染,并将参数附加到目标上。虽然这不是当前问题的实质,但对未来的大型项目来说是一个好的实践。

在此处调用link_directories是无用的。如果需要链接库,请优先使用target_link_libraries

EXECUTABLE_OUTPUT_PATH是一个旧变量,已经被RUNTIME_OUTPUT_DIRECTORY取代,这是一个目标属性。

清理后,您的CMake项目可能如下所示:

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 )

2
您需要使用CMake的“2.8”版本,这个版本太低了,不能使用Qt的一些特性,比如导入目标和在Windows上自动连接qtmain,这正是策略警告的作用所在。
相反,建议使用更高版本的CMake。
cmake_minimium_required(VERSION 2.8.11)

对于这个版本,不再需要使用FATAL_ERROR选项。

请注意,即使您拥有CMake 2.8.12,它也会模拟您在cmake_minimium_required命令中编写的确切版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接