41

I have a .pro file on my project, but now I want to port it to a CMakeLists.txt file. How can I do this?

QT += core
QT -= gui
CONFIG += c++11
TARGET = test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
QT += network
SOURCES += main.cpp \
    test_interface.cpp \
    motomanlibrary.cpp \
    processing.cpp
SOURCES += main.cpp \
    test_interface.h \
    motomanlibrary.h \
    processing.h
Kevin
  • 16,549
  • 8
  • 60
  • 74

3 Answers3

41

QMake: The required libraries.

QT += core
QT -= gui
QT += network

CMake: only the add is necessary. An exclude (QT -= gui) is not required.

find_package(Qt5Core REQUIRED)
find_package(Qt5Network REQUIRED)

QMake: Additional Compiler flags:

CONFIG += c++11

CMake: Extend the list of compiler flags as required.

set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -std=c++0x")

QMake: The source files

SOURCES += main.cpp \
    test_interface.cpp \
    motomanlibrary.cpp \
    processing.cpp

CMake: Create a list of source files

set(SOURCES
    main.cpp
    test_interface.cpp
    motomanlibrary.cpp
    processing.cpp
)

QMake: The header to be included:

SOURCES += main.cpp \
    test_interface.h \
    motomanlibrary.h \
    processing.h

CMake: Just show where the header files are.

include_directory(.) #  or include_directory(${CMAKE_CURRENT_SOURCE_DIR})
include_directory(some/where/else)

QMake: The target to be built:

TARGET = test

CMake: Set the name of the target, add the sources, link the required libs.

add_executable(test ${SOURCES} )
qt5_use_modules(test Core Network) # This macro depends from the Qt version

# Should not be necessary
#CONFIG += console
#CONFIG -= app_bundle
#TEMPLATE = app

See further details on Convert qmake to cmake

Th. Thielemann
  • 2,592
  • 1
  • 23
  • 38
  • 4
    I believe the "new" recommended way to add Qt ilbs to Cmake project is using `target_link_libraries`, eg. `target_link_libraries(test Qt5::Core)` and `target_link_libraries(test Qt5::Network)`, etc. Ref: https://doc.qt.io/qt-5/cmake-get-started.html and https://doc.qt.io/qt-5/cmake-command-reference.html – Maxim Paperno Oct 01 '19 at 01:06
11

There is a python script to convert QMake to CMake on a WIP branch of Qt Base: https://code.qt.io/cgit/qt/qtbase.git/tree/util/cmake/pro2cmake.py?h=wip/cmake

It will probably be released with Qt 6 when CMake will become the main build system.

Andrea Ricchi
  • 480
  • 5
  • 12
  • Since is in an alpha stage I didn't use it. I suppose it works by parsing all the `.pro` and `.pri` and converting the QMake keywords to CMake. I think that for simple projects it could work well, but when you start to have multiple modules with `.pro` and maybe an automatic dependencies resolver like QtCreator it may fail! – Andrea Ricchi Nov 16 '19 at 17:13
  • I found a small wiki for the module , but not very detailed , see comments. – Mohammad Kanan Nov 16 '19 at 17:47
  • 1
    In my case `pro2cmake.py` had infinite loop. Turns out it requires `.qmake.conf`, and to bypass such limit, while-loop in `pro2cmake.py:find_qmake_conf` must be modified to stop as soon as `cwd` is the root (I just checked if previous CWD is the same as current to `break` out of the loop). – Pugsley Oct 26 '20 at 07:09
  • 2
    now has its own repo at https://code.qt.io/cgit/qt/qmake2cmake.git/ – milahu Jun 08 '22 at 16:28
0

This is what you would typically write in a simple Qt application built with cmake consuming Qt. A few gotchas:

  1. Use automoc. This reduces the maintenance overhead significantly. It is also fast enough even for large projects so that you do not need to care about build-time slow-down.

  2. Use versionless cmake targets so that they are compatible across Qt versions.

  3. If you can, take advantage of the QT_DISABLE_DEPRECATED_BEFORE variable.

  4. As a bonus, enable some usual warning, error and sanitizer detections if you aim for high quality.

  5. You may or may not want to add a Qt 5 fallback in case anyone would like to use your software with Qt 5. This requirement may phase out slowly, but surely.

    project(Application VERSION 1.0.0 LANGUAGES CXX)                                   
    
    set(CMAKE_CXX_STANDARD 23)                                                      
    set(CMAKE_CXX_STANDARD_REQUIRED ON)                                             
    
    set(CMAKE_AUTOMOC ON)                                                           
    set(CMAKE_AUTORCC ON)                                                           
    set(CMAKE_AUTOUIC ON)                                                           
    
    find_package(Qt6 COMPONENTS Widgets)                  
    if (NOT Qt6_FOUND)                                                              
      find_package(Qt5 5.15 REQUIRED COMPONENTS Widgets)                
    endif()
    
    if (CMAKE_CXX_COMPILER_ID MATCHES "(Clang|GNU)")                              
      add_compile_options(-Wall -Wpedantic -Wextra -Werror)                       
      add_compile_options(-fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer)
      add_link_options(-fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer)
    elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")                                 
      set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "/w")                
    endif()                                                                       
    
    add_compile_definitions(QT_DISABLE_DEPRECATED_BEFORE=0xFFFFFF)
    
    add_executable(Application                                                                                                                     
      mainwindow.cpp                                                                
      main.cpp                                                                                                                              
    )
    
    target_link_libraries(Application Qt::Widgets)
    
    
László Papp
  • 51,870
  • 39
  • 111
  • 135