Adding QT5 libraries to CMake

12,727

Solution 1

Some tips:

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

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

find_package(Qt5 REQUIRED
    Core
    PrintSupport
    Gui
    Widgets
)

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 and Gui are optional here since Widgets depends on them, so they will be added automatically.

You should change your required version for CMake to 2.8.11 to ensure imported targets embed 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.

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

EXECUTABLE_OUTPUT_PATH is an old variable, superceded by RUNTIME_OUTPUT_DIRECTORY which is a target property.

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 )

Solution 2

You require "2.8" version of CMake, which is too low for use features of Qt like imported targets and automatical linking to qtmain on Windows. This is exactly what policy warnings are about.

Instead use

cmake_minimium_required(VERSION 2.8.11)

(FATAL_ERROR option is no longer needed for this version).

Note, that even you have CMake 2.8.12, it emulates exact version which you write in cmake_minimium_required command.

Share:
12,727
ns533
Author by

ns533

Updated on June 18, 2022

Comments

  • ns533
    ns533 almost 2 years

    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

    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 )
    

    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
    

    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?