简体   繁体   English

将 QMake 项目转换为 CMake

[英]Converting QMake project to CMake

I recently started to refresh my C++ knowledge (haven't used it for like 10 years~).我最近开始刷新我的 C++ 知识(10 年没用了~)。 I chose to use QtCreator, since I wanted to learn more about Qt which I only tried once.我选择使用 QtCreator,因为我想了解更多关于 Qt 的信息,我只尝试了一次。

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.现在,我创建了一个 qmake 项目,一切正常,但我也想了解 cmake,为此我考虑将项目转换为 cmake。

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.不幸的是,我对 C++ 的构建过程的了解非常少,我通常只让 IDE 为我处理所有这些事情,因为我的项目很小。

Now, I would like to ask you to help me move my qmake.pro file to cmake CMakeLists.txt, it's fairly simple.现在,我想请你帮我把我的qmake.pro文件移动到cmake CMakeLists.txt,这很简单。

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.我通常不喜欢获得“即插即用”解决方案,但在深入研究 cmake 之前,我希望将其转换并能够构建它。

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)?至于构建,这是一个不同的问题,但也许有人也可以回答这个问题——我是否需要在 QtCreator 中进行任何更改以使其在我的项目中使用 cmake 而不是 qmake(或者理想情况下将它们并排放置以可以在 cmake 和 qmake 之间切换)?

I already have CMake added in Build & Run -> CMake (it was autodetected).我已经在 Build & Run -> CMake 中添加了 CMake(它是自动检测到的)。

The difference between qmake and cmake for Qt programing is that qmake already knows where to find the Qt Libraries and Sources. Qmake程序的qmake和cmake之间的区别在于qmake已经知道在哪里可以找到Qt库和源代码。 CMake doesn't know that and you probably have point to right location of the library. CMake不知道这一点,您可能已指向库的正确位置。 Usually you will declare a environment variable QT_DIR. 通常,您将声明一个环境变量QT_DIR。 I will put an example using Qt5 since I forgot how to create a Qt4 project in cmake 我将使用Qt5作为示例,因为我忘记了如何在cmake中创建Qt4项目。

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 QtCreator已经支持cmake项目,您只需要打开其中包含CMakeLists.txt的文件夹,IDE就会将其识别为cmake项目。

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或者,对于这样一个简单的项目(一个可执行文件,没有库),您可以使用我的脚本cmake-project ,它等效于qmake -project -project : https://github.com/KDAB/KDToolBox/tree/master/qt/ cmake项目

Just run it in your source dir and it will generate a CMakeLists.txt to build all sources as a single executable.只需在您的源目录中运行它,它将生成一个CMakeLists.txt以将所有源构建为一个可执行文件。

One may consider using qmake2cmake .可以考虑使用qmake2cmake

These are Python scripts provided by Qt that enables to convert a single pro file or a whole project tree.这些是由 Qt 提供的 Python 脚本,可以转换单个pro文件或整个项目树。

qmake2cmake ~/projects/myapp/myapp.pro will just convert myapp.pro to a CMakeLists.txt file along myapp.pro . qmake2cmake ~/projects/myapp/myapp.pro只会将myapp.pro转换为myapp.pro中的CMakeLists.txt文件。

qmake2cmake_all ~/projects/myapp will convert the whole project tree under ~/projects/myapp qmake2cmake_all ~/projects/myapp将转换整个项目树在~/projects/myapp

Refer to qmake2cmake for more details详情请参考qmake2cmake

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM