C++ CMake和qmake有什么区别和相似之处?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34370088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences and similarities between CMake and qmake?
提问by Hareen Laks
I would like to know reasons for use CMake for particular project over qmake and vice versa.
我想知道 在 qmake 上为特定项目使用 CMake 的原因,反之亦然。
Simply what are the pros and cons of both build systems? I searched and found few documents but feel difficult to understand.
两种构建系统的优缺点是什么?我搜索并找到了一些文件,但感觉很难理解。
回答by Humpparitari
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.
两者都是构建系统,但它们根本不是很相似。如果您的项目使用 Qt,您可能最好使用 qmake。CMake 更通用,几乎适用于任何类型的项目。
Both qmake and CMake generate a Makefile, which is read by make
to build the project. Not all build systems generate a Makefile, but many do. Even a Makefile is a type of build system; it tells the compiler and linker what to do, in order to create an executable (or a dynamic or static library).
qmake 和 CMake 都会生成一个Makefile,它被读取make
以构建项目。并非所有构建系统都会生成 Makefile,但很多都会生成。甚至 Makefile 也是一种构建系统;它告诉编译器和链接器要做什么,以创建可执行文件(或动态或静态库)。
If your project uses Qt, but you don't want to use qmake, you'll have to do a few more things yourself:
如果你的项目使用Qt,但又不想使用qmake,那你就得自己多做几件事:
- running the Meta Object Compiler(MOC)
- include paths (tell the compiler where to look for Qt headers)
- linking (tell the linker where to look for Qt libraries)
- 运行元对象编译器(MOC)
- 包含路径(告诉编译器在哪里寻找 Qt 头文件)
- 链接(告诉链接器在哪里寻找 Qt 库)
So, you'll have to do a bit more work to build a Qt project without qmake, but it is possible and it will teach you a lot about how Qt and qmake do things.
因此,您必须做更多的工作才能在没有 qmake 的情况下构建 Qt 项目,但这是可能的,并且它会教您很多关于 Qt 和 qmake 如何做事的知识。
On a personal note (take this only as a recommendation, do further research yourself): I'm not a big fan of qmake. It helps you with the Qt stuff, but apart from that, I've found it to be pretty limited.
就个人而言(仅将其作为建议,请自行进一步研究):我不是 qmake 的忠实粉丝。它可以帮助您处理 Qt 的内容,但除此之外,我发现它非常有限。
In any case, I would recommend learning to build a small project (~10 source files) without using any type of build system. Not using CMake, not with a Makefile, just using the compiler and linker directly. You shouldn't actually build any real project in this way, but you should learn how to do it, just to learn what build systems actually do. Knowing what they do will make them a lot easier to use.
在任何情况下,我都建议学习构建一个小项目(约 10 个源文件)而不使用任何类型的构建系统。不使用 CMake,不使用 Makefile,直接使用编译器和链接器。您实际上不应该以这种方式构建任何真正的项目,但您应该学习如何去做,只是为了了解构建系统实际上做了什么。了解它们的作用将使它们更易于使用。
A few months ago, we switched a project from qmake to Premake, which is also worth a look. It's highly scriptable (with Lua), which is great when you need to customize your build process.
几个月前,我们将一个项目从 qmake 切换到Premake,这也值得一看。它是高度可编写脚本的(使用 Lua),这在您需要自定义构建过程时非常有用。
That said, it is a bit more "manual", so prepare yourself to learn how compiling and linking works at a more basic level, without the use of a build system. It's also in beta (Premake 5), so there are some bits and pieces still missing.
也就是说,它更像是“手动”,因此请准备好在更基本的级别上了解编译和链接的工作原理,而无需使用构建系统。它也处于测试阶段(Premake 5),所以仍然缺少一些零碎的东西。
You can also take a look at qbs, which is supposed to be a better qmake. It's still in a beta stage, so I'd wait while it matures and becomes easier to use.
你也可以看看qbs,它应该是一个更好的 qmake 。它仍处于测试阶段,所以我会等待它成熟并变得更容易使用。
回答by Frank Osterfeld
CMake is by far the more powerful build system. The syntax is "not so nice" to put it mildly. But then, for any complex project, what one has to do with QMake (or any buildsystem I know of) to achieve things isn't nice either. For simple projects, QMake is nicer to look at though.
CMake 是迄今为止功能更强大的构建系统。委婉地说,语法“不太好”。但是,对于任何复杂的项目,使用 QMake(或我知道的任何构建系统)来实现目标也不好。对于简单的项目,QMake 更好看。
If you need configure checks for third-party dependencies other than Qt, CMake is what you want, support for configure checks in QMake is minimal to non-existent.
如果您需要对 Qt 以外的第三方依赖项进行配置检查,CMake 就是您想要的,QMake 中对配置检查的支持很少甚至不存在。
QMake on the other hand works great with Qt Creator (CMake support in there is lacking, although it's doable to use CMake with Creator).
另一方面,QMake 与 Qt Creator 配合得很好(缺乏对 CMake 的支持,尽管将 CMake 与 Creator 一起使用是可行的)。
If you want to build and deploy for iOS and Android from within Qt Creator, I strongly suggest QMake. (Not sure if it's even possible these days with CMake - it will be certainly cause a lot more headache).
如果您想在 Qt Creator 中为 iOS 和 Android 构建和部署,我强烈建议使用 QMake。(不确定这些天是否可以使用 CMake - 它肯定会引起更多的头痛)。
回答by Mr Squid
I use CMake for my Qt projects and am very happy with it. Specifically, I have the following in my CMakeLists.txt:
我将 CMake 用于我的 Qt 项目并且对它非常满意。具体来说,我的 CMakeLists.txt 中有以下内容:
set(QT_VERSION_REQ "5.2")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Core ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Quick ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Widgets ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Gui ${QT_VERSION_REQ} REQUIRED)
set(CMAKE_AUTOMOC ON)
QT5_WRAP_UI( UI_HDRS ${UI_FILES} )
ADD_EXECUTABLE(${MOC_HEADERS})
target_link_libraries(${PROJECT_NAME}
Qt5::Core
Qt5::Quick
Qt5::Widgets
Qt5::Gui
)
I hope this helps if you do decide to go with CMake.
如果您决定使用 CMake,我希望这会有所帮助。
回答by jpo38
CMake
will help you generating configuration files for many build systems (a "build system" is called Generators in CMake
). See What is a CMake generator?.
CMake
将帮助您为许多构建系统生成配置文件(“构建系统”在 中称为生成器CMake
)。请参阅什么是 CMake 生成器?.
It means that if you have a set of C/C++ sources and a well written CMakeLists.txt
, you can use CMake to create projects for different build system (IDE based on command-line based) like Visual Studio, CodeBlocks, g++...
With CMake, you can choose the IDE and compiler you will use in the end and 'easily' switch between supported ones.
这意味着如果你有一套 C/C++ 源代码和一个写得很好的 . CMakeLists.txt
,你可以使用 CMake 为不同的构建系统(基于命令行的 IDE)创建项目,如 Visual Studio、CodeBlocks、g++...使用 CMake ,您可以选择最终将使用的 IDE 和编译器,并“轻松”地在支持的之间切换。
qmake
, as far as I know, will only support QtCreator as an IDE (itleft using 3rd party compiler in the background: nmake from Visual Studio, g++, MinGW...). But you won't generate a Visual Studio solution (sln file) using qmake
. That's the main limitation I see in qmake (because I hate QtCreator...Visual Studio is way more powerful and intuitive...but that's just my opinion).
qmake
,据我所知,只支持 QtCreator 作为 IDE(它在后台使用 3rd 方编译器:来自 Visual Studio、g++、MinGW 的 nmake ......)。但是您不会使用qmake
. 这是我在 qmake 中看到的主要限制(因为我讨厌 QtCreator...Visual Studio 更强大和更直观...但这只是我的意见)。
It's true qmake
makes it easier to compile Qt-based applications (because MOC, .ui and liking to Qt is natively supported) than CMake
. But it's doable anyway using CMake
(there's built-in functions for Qt integration). Only your CMakeLists.txt may have to be reworked a bit when moving to new releases of Qt (I had a hard time when moving from Qt4 to Qt5, I guess that using qmake
makes this easier).
的确qmake
,编译基于 Qt 的应用程序比CMake
. 但无论如何使用它都是可行的CMake
(有用于 Qt 集成的内置函数)。只有你的 CMakeLists.txt 可能需要在移动到 Qt 的新版本时稍微修改一下(从 Qt4 移动到 Qt5 时我很难过,我想使用qmake
会使这更容易)。
Personnaly, I use CMake
for my build environment and, when I need to use QtCreator (for Android deployment mainly), I make my CMake
scripts generate a .pro file for qmake/QtCreator. It works well because qmake
.pro file syntax is really simple. Then I'm not locked to a specific IDE.
Personnaly,我CMake
用于我的构建环境,当我需要使用 QtCreator(主要用于 Android 部署)时,我让我的CMake
脚本为 qmake/QtCreator 生成一个 .pro 文件。它运行良好,因为qmake
.pro 文件语法非常简单。然后我没有被锁定到特定的 IDE。
回答by Th. Thielemann
qmake
qmake
- Has focus on projects using Qt
- Project file is easily generated by QtCreator (good for beginners)
- Supported by QtCreator
- 专注于使用 Qt 的项目
- QtCreator 很容易生成项目文件(适合初学者)
- 由 QtCreator 支持
CMake
制作
- Is used in a wide range of projects
- Supports a lot of platforms and languages
- Supported by multiple IDE's: e.g. QtCreator, Visual Studio
- Generates project description for multiple IDE's
- Contains commands to make usage of Qt easy (Most important: automoc)
- 在广泛的项目中使用
- 支持多种平台和语言
- 受多个 IDE 支持:例如 QtCreator、Visual Studio
- 为多个 IDE 生成项目描述
- 包含使 Qt 使用更容易的命令(最重要的是:automoc)
My recommendation: Use qmake in case QtCreator is your IDE and you starts with Qt or C++. Use cmake in case you want to do any complex stuff in your build.
我的建议:如果 QtCreator 是您的 IDE 并且您从 Qt 或 C++ 开始,请使用 qmake。如果您想在构建中执行任何复杂的操作,请使用 cmake。
Both qmake and CMake work similary. See http://www.th-thielemann.de/development/cmake/cmake_qmake_to_cmake.htmlfor a tutorial to migrate from qmake to CMake.
qmake 和 CMake 的工作方式相似。有关从 qmake 迁移到 CMake 的教程,请参见http://www.th-thielemann.de/development/cmake/cmake_qmake_to_cmake.html。