Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding QT5 libraries to CMake

Tags:

c++

cmake

qt

qt5

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?

like image 737
ns533 Avatar asked Nov 03 '16 01:11

ns533


People also ask

How does CMake find Qt5?

In order for find_package to be successful, Qt 5 must be found below the CMAKE_PREFIX_PATH, or the Qt5_DIR must be set in the CMake cache to the location of the Qt5WidgetsConfig. cmake file. The easiest way to use CMake is to set the CMAKE_PREFIX_PATH environment variable to the install prefix of Qt 5.

Where is Qt6 config CMake?

This time, I found a Qt6Config. cmake there (inside C:\Qt\6.3. 1\msvc2019_64\lib\cmake\Qt6).

Does Qt use CMake?

Qt relies on some bundled tools for code generation, such as moc for meta-object code generation, uic for widget layout and population, and rcc for virtual file system content generation. These tools may be automatically invoked by cmake(1) if the appropriate conditions are met.

What is the difference between Qmake and CMake?

Both are build systems, but they're not very similar at all. If your project uses Qt, you're probably best off using qmake. CMake is more generic, and fits pretty much any type of project. Both qmake and CMake generate a Makefile, which is read by make to build the project.


2 Answers

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 )
like image 98
rgmt Avatar answered Oct 21 '22 21:10

rgmt


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.

like image 39
Tsyvarev Avatar answered Oct 21 '22 22:10

Tsyvarev