Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting QMake project to CMake

Tags:

c++

cmake

qt

qmake

I recently started to refresh my C++ knowledge (haven't used it for like 10 years~). I chose to use QtCreator, since I wanted to learn more about Qt which I only tried once.

Now, I created a qmake project and it all works just fine, but I would also like to learn about cmake and I thought of converting the project to cmake for that purpose.

Unfortunately my knowledge related to building process of C++ is very small, I usually just let IDE to handle all that stuff for me, since my projects were rather small.

Now, I would like to ask you to help me move my qmake .pro file to cmake CMakeLists.txt, it's fairly simple.

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = model_view_playground
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

QMAKE_CXXFLAGS += -std=c++0x

SOURCES += main.cpp\
        mainwindow.cpp \
    testitemlistmodel.cpp

HEADERS  += mainwindow.h \
    testclass.h \
    testitemlistmodel.h

FORMS    += mainwindow.ui

DISTFILES +=

I do not like to get "plug and play" solutions usually, but before I dive into cmake, I'd like to have this converted and be able to build it.

As for building, it's kind of a different question, but perhaps someone can answer this one as well - do I need to make any changes in QtCreator to make it use cmake instead of qmake in my project (or ideally keep them side by side to be able to switch between cmake and qmake)?

I already have CMake added in Build & Run -> CMake (it was autodetected).

like image 946
pzaj Avatar asked Oct 28 '25 12:10

pzaj


2 Answers

The difference between qmake and cmake for Qt programing is that qmake already knows where to find the Qt Libraries and Sources. CMake doesn't know that and you probably have point to right location of the library. Usually you will declare a environment variable QT_DIR. I will put an example using Qt5 since I forgot how to create a Qt4 project in cmake

project(Qt5Project) # Your project name

set(CMAKE_CXX_STANDARD 11) # This is equal to QMAKE_CXX_FLAGS += -std=c++0x

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Instruct CMake to run uic automatically when needed.
set(CMAKE_AUTOUIC ON)

# This will find the Qt5 files. You will need a QT5_DIR env variable
find_package(Qt5Widgets REQUIRED) # Equivalent of QT += widgets

set(SOURCES main.cpp mainwindow.cpp testitemlistmodel.cpp) 
set(HEADERS mainwindow.h testclass.h testitemlistmodel.h)
set(UI mainwindow.ui)

# This will create you executable
add_executable(model_view_playground ${SOURCES} ${HEADERS} ${UI})
# This will link necessary Qt5 libraries to your project
target_link_libraries(model_view_playground Qt5::Widgets)

For a simple project a script like this will serve.

QtCreator already has support for cmake projects, you only need to open a folder with a CMakeLists.txt inside it and the IDE will recognize as a cmake project

like image 68
Caio Belfort Avatar answered Oct 31 '25 03:10

Caio Belfort


Alternatively, for such a simple project (one executable, no libs) you could use my script cmake-project, which is an equivalent of qmake -project : https://github.com/KDAB/KDToolBox/tree/master/qt/cmake-project

Just run it in your source dir and it will generate a CMakeLists.txt to build all sources as a single executable.

like image 27
David Faure Avatar answered Oct 31 '25 03:10

David Faure



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!