c – 将QT5库添加到CMake

我是新手使用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.有什么我做错了吗?

解决方法:

一些技巧:

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?

Core和Gui在这里是可选的,因为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 )
上一篇:c – 如何制作QheaderView多级?


下一篇:c – 如何在Qt Creator 4.2.0(社区)中包含QtCharts库