cmake编译qt项目

#实际上是调用cmake安装目录\share\cmake-2.8\Modules\FindQt4.cmake
#设置好一批预定义的东东
FIND_PACKAGE(Qt4 REQUIRED)

# 不解释
SET(helloworld_SOURCES main.cpp hellowindow.cpp)
SET(helloworld_HEADERS hellowindow.h)

#处理helloworld_HEADERS中的MOC宏,处理结果生成在helloworld_HEADERS_MOC
QT4_WRAP_CPP(helloworld_HEADERS_MOC ${helloworld_HEADERS})

#添加QT头文件和宏
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})






**********************************************

Using CMake to Build Qt Projects

Written by: Johan Thelin

Qt comes with the QMake tool for handling cross platform building issues. However, there are other build systems available such as autotools, SCons and CMake. These tools meet different criterias, for example external dependencies.

When the KDE project shifted from Qt 3 to Qt 4 the project changed build tool from autotools to CMake. This has given CMake a special position in the Qt world &emdash; both from the number of users point and from a feature support and quality point. Seen from a workflow point of view, Qt Creator supports CMake since version 1.1 (1.3 if you want to use a Microsoft toolchain).

A Basic Example

In this article we will focus on CMake itself, and how to use it in conjunction with Qt. To do this, let's start with an overview of a simple, but typical CMake-based project. As you can tell from the listing below, the project consists of some source files and a text file.

$ ls CMakeLists.txt hellowindow.cpp hellowindow.h main.cpp 

Basically, the CMakeLists.txt file replaces the projectfile used by QMake. To build the project, create a build directory and run cmake and then make from there. The reason for creating a build directory is that CMake has been built with out-of-source building in mind from the very start. It is possible to configure QMake to place intermediate files outside the source, but it requires extra steps. With CMake, it is the default.

$ mkdir build $ cd build $ cmake .. && make 
The argument given to CMake refers to the directory where the CMakeLists.txt file resides. This file controls the whole build process. In order to fully understand it, it is important to recognize how the build process looks. The figure below shows how the user files: sources, headers, forms and resource files are processed by the various Qt code generators before joining the standard C++ compilation flow. Since QMake was designed to handle this flow, it hides all the details of this flow.

When using CMake, the intermediate steps must be handled explicitly. This means that headers with Q_OBJECT macros must be run through moc, user interface forms must be processed by uic and resource files must pass through rcc.

In the example that we started with the world is slightly easier, though. It is limited to a single header file that needs to meet moc. But first, the CMakeLists.txt defines a project name and includes the Qt4 package as a required component.

PROJECT(helloworld) FIND_PACKAGE(Qt4 REQUIRED) 

Then all sources involved in the build process are assigned to two variables. The SET command assigns the variable listed first with the values that follow. The names, helloworld_SOURCES and helloworld_HEADERS, is by convention. You can name them either way you like.

SET(helloworld_SOURCES main.cpp hellowindow.cpp)
SET(helloworld_HEADERS hellowindow.h) 

Notice that the headers only include the headers that needs to be processed by moc. All other headers can be left out of the CMakeLists.txt file. This also implicates that if you add a Q_OBJECT macro to any of your classes you must ensure that it is listed here.

To invoke moc, the macro QT4_WRAP_CPP is used. It assigns the names of the resulting files to the variable listed first. In this case the line looks as follows.

QT4_WRAP_CPP(helloworld_HEADERS_MOC ${helloworld_HEADERS}) 

What happens is that all headers are processed by moc and the names of the resulting source files are listed in the helloworld_HEADERS_MOC variable. Again, the variable name is by convention rather than forced.

In order to build a Qt application, the Qt include directories needs to be added as well as a range of defines need to be set. This is handled through the commands INCLUDE and ADD_DEFINITIONS.

INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS}) 

Finally, CMake needs to know the name of the resulting executable and what to link it to. This is conveniently handled by by the commands ADD_EXECUTABLE and TARGET_LINK_LIBRARIES. Now CMake knows what to build, from what and through which steps.

ADD_EXECUTABLE(helloworld ${helloworld_SOURCES}  ${helloworld_HEADERS_MOC})
TARGET_LINK_LIBRARIES(helloworld ${QT_LIBRARIES}) 

When reviewing the listing above, it relies on a number of variables starting with QT_. These are generated by the Qt4 package. However, as a developer, you must explicitly refer to them as CMake is not build as tightly to suite Qt as QMake.

Adding More Qt

Moving beyond the initial example, we now look at a project with both resources and user interface forms. The resulting application will look quite similar to its predecessor, but all the magic takes place under the hood.

The CMakeLists.txt file start by naming the project and including the Qt4 package - the complete file can be downloaded as a source code package accompanying this article. Then all the input files are listed and assigned to their corresponding variables.

SET(helloworld_SOURCES main.cpp hellowindow.cpp)
SET(helloworld_HEADERS hellowindow.h)
SET(helloworld_FORMS hellowindow.ui)
SET(helloworld_RESOURCES images.qrc) 

The new file types are then handled by QT4_WRAP_UI and QT4_ADD_RESOURCES. These macros operate in the same ways as QT4_WRAP_CPP. This means that the resulting files are assigned to variable given as the left-most argument. Notice that the header files generated by uic are needed as we need to build a dependency relationship between them and the final executable. Otherwise they will not be created.

QT4_WRAP_CPP(helloworld_HEADERS_MOC ${helloworld_HEADERS})
QT4_WRAP_UI(helloworld_FORMS_HEADERS ${helloworld_FORMS})
QT4_ADD_RESOURCES(helloworld_RESOURCES_RCC ${helloworld_RESOURCES}) 

All the resulting files are then added as dependencies to the ADD_EXECUTABLE macro. This includes the uic generated headerfiles. This establishes the dependency from the executable to the hellowindow.ui file through the intermediary ui_hellowindow.h header.

ADD_EXECUTABLE(helloworld
	${helloworld_SOURCES}
	${helloworld_HEADERS_MOC}
	${helloworld_FORMS_HEADERS}
	${helloworld_RESOURCES_RCC}
) 

Before this file can be used to build the project there is a small caveat to handle. As all intermediate files are generated outside the source tree, the header file generated by uic will not be located by the compiler. In order to handle this, the build directory needs to be added to the list of include directories.

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) 

With this line, all intermediary files will be available in the include path.

Qt Modules

Until now we have relied on the QtCore and QtGui modules. To be able to use more modules, the CMake environment must be tuned to enable them. By setting them to TRUE using the SET command, the included modules can be controlled.

For instance, to enable OpenGL support add the following line to your CMakeLists.txt.

SET(QT_USE_QTOPENGL TRUE) 

The most commonly used modules are controlled using the following variables.

  • QT_USE_QTNETWORK
  • QT_USE_QTOPENGL
  • QT_USE_QTSQL
  • QT_USE_QTXML
  • QT_USE_QTSVG
  • QT_USE_QTTEST
  • QT_USE_QTDBUS
  • QT_USE_QTSCRIPT
  • QT_USE_QTWEBKIT
  • QT_USE_QTXMLPATTERNS
  • QT_USE_PHONON

In addition to these, the variable QT_DONT_USE_QTGUI can be used to disable the use to QtGui. There is a similar variable to disable QtCore, but that is more to be feature complete than to actually add much useful value.

Added Value and Complexity

It is not as trivial to use CMake as QMake, but the rewards are more features. The most notable point when moving from QMake is CMake's built in support for out-of-source builds. It can really change habits, and thus make versioning code much easier.

It is also possible to add conditionals for various platforms and build scenarios. For instance, use different libraries for different platforms, as well as tuning the same project differently for different situations.

Other powerful features are the ability to generate multiple executables and libraries in one build as well as using said executables and libraries in the same build. This, in combination with the QtTest module can handle complex build situations from a single configuration.

The choice between CMake and QMake is really quite easy. For straight forward Qt projects, QMake is the obvious choice. When the build requirements passes the complexity threshold for QMake, CMake can take its place. With Qt Creator's support for CMake, the same tools can still be used.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Windows 下使用 CMake 编译 Qt 可以分为以下几个步骤: 1. 安装 Qt 首先需要安装 Qt,可以从官网下载安装程序进行安装。安装完成后,需要设置环境变量 QTDIR,指向 Qt 的安装目录。 2. 安装 CMake CMake 是一个跨平台的开源构建工具,可以从官网下载安装程序进行安装。 3. 配置 CMake 在配置 CMake 之前,需要先创建一个 CMakeLists.txt 文件,该文件描述了项目的构建过程。下面是一个简单的 CMakeLists.txt 文件示例: ``` cmake_minimum_required(VERSION 3.5) project(MyProject LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt5 COMPONENTS Widgets REQUIRED) add_executable(MyApp main.cpp) target_link_libraries(MyApp Qt5::Widgets) ``` 上面的示例文件中,指定了项目的名称和编程语言为 C++,并且使用了 Qt 的 Widgets 模块。最后创建了一个可执行文件 MyApp,并链接了 Qt5::Widgets 库。 接下来,在命令行中进入项目目录,执行以下命令进行配置: ``` mkdir build cd build cmake .. ``` 上述命令中,首先创建一个 build 目录,然后进入该目录,最后执行 cmake 命令进行配置。配置完成后,会在 build 目录中生成 Makefile 文件。 4. 编译项目 在命令行中执行以下命令进行编译: ``` make ``` 编译完成后,会在 build 目录中生成可执行文件 MyApp。 以上就是在 Windows 下使用 CMake 编译 Qt 的基本步骤。注意,上述命令中涉及到的路径需要根据实际情况进行修改。 ### 回答2: 在Windows下使用CMake编译Qt可以采取以下步骤: 1. 安装Qt:首先,需要在计算机上安装Qt开发框架。可以从Qt官方网站下载适用于Windows的Qt安装程序,并按照提示完成安装。 2. 安装CMake:接下来,需要在计算机上安装CMake工具。可以从CMake官方网站下载适用于Windows的CMake安装程序,并按照提示完成安装。 3. 创建CMakeLists.txt文件:在项目的根目录下创建一个CMakeLists.txt文件。该文件包含描述如何构建项目的指令。 4. 配置CMake:使用命令行或图形界面工具打开CMake,并指定项目的源代码和构建目录。将源代码目录指向包含CMakeLists.txt文件的根目录,并选择构建目录,用于生成构建文件。 5. 生成构建文件:点击"Configure"按钮,CMake将解析CMakeLists.txt文件,并根据指令生成构建文件。可以选择生成Makefile、Visual Studio工程文件或其他支持的构建系统文件。 6. 编译项目:点击"Generate"按钮,CMake将生成所选构建系统所需的文件,可以在构建目录中找到。 7. 构建项目:使用所选的构建系统工具(例如,在Visual Studio中打开工程文件,或在命令行中使用make命令),进行实际的编译和构建过程。 8. 运行项目:完成编译后,可以在生成的可执行文件或库文件中找到生成的Qt应用程序,并运行它。 注意:在CMakeLists.txt文件中,应该指定项目需要的Qt模块和库,并确保CMake能够找到所需的Qt安装路径。可以使用find_package(Qt5 COMPONENTS ...)指令来查找并链接所需的Qt库。同时还可以设置其他构建选项,如编译器标志、链接选项等。 总之,使用CMake编译Qt项目可以提供更灵活和可移植的构建过程,可以轻松管理和跨平台编译Qt应用程序。 ### 回答3: 在Windows操作系统下使用CMake编译Qt有以下几步: 1. 首先确保已经安装了CMakeQt。可以从官方网站上下载并安装它们。 2. 创建一个新的CMakeLists.txt文件,该文件会告诉CMake如何构建和编译我们的Qt项目。 3. 在CMakeLists.txt文件中,添加以下内容: ``` cmake_minimum_required(VERSION 3.5) project(MyQtProject) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON) if(WIN32) set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.15.2/5.15.2/msvc2019_64") #设置Qt安装路径 endif() find_package(Qt5 COMPONENTS Core Gui Widgets REQUIRED) file(GLOB_RECURSE SOURCES src/*.cpp) file(GLOB_RECURSE HEADERS include/*.h) add_executable(MyQtProject ${SOURCES} ${HEADERS}) target_link_libraries(MyQtProject PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets) ``` 其中的"project(MyQtProject)"可以根据实际项目名称进行修改。"set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.15.2/5.15.2/msvc2019_64")"是设置Qt的安装路径,需要根据自己的实际安装路径进行修改。 4. 在项目根目录下创建一个build文件夹,用于存放生成的构建文件。 5. 打开命令提示符或者使用CMake GUI,并进入build文件夹的路径。 6. 在命令提示符或CMake GUI中执行以下命令来生成构建文件: ``` cmake .. ``` 7. 构建生成的构建文件,执行以下命令(如果使用CMake GUI,则可以直接点击“Generate”按钮): ``` cmake --build . ``` 8. 构建完成后,我们就可以在build文件夹中找到编译生成的可执行文件。 这样,我们就成功使用CMake编译Qt项目了。记得根据自己的实际情况修改相应的路径和文件名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值