Xmake recently added support for the Qt SDK environment, and now we can completely develop Qt applications without Qt Creator. Xmake-vscode, xmake-idea, xmake-sublime… , users can switch to their most common editor environment to develop and build Qt applications, for example:

Create an empty project from a template

Xmake includes several built-in project templates that can be used to quickly create empty Qt-based projects, such as:

$ xmake create -l c++ -t console_qt test
$ xmake create -l c++ -t static_qt test
$ xmake create -l c++ -t shared_qt test
$ xmake create -l c++ -t quickapp_qt test
Copy the code

Currently, four engineering templates are mainly provided, corresponding to console program, static library, dynamic library, and UI application program.

Take the QuickApp project as an example. The resulting empty project xmake. Lua will look something like this:

target("qt_demo")

    -- add rules
    add_rules("qt.application")

    -- add headers
    add_headers("src/*.h")

    -- add files
    add_files("src/*.cpp") 
    add_files("src/qml.qrc")

    -- add frameworks
    add_frameworks("QtQuick")
Copy the code

Qt SDK environment configuration

By default, xmake will automatically detect the Qt environment. However, if the Qt SDK environment is not found, users can manually specify the Qt SDK environment directory:

$Xmake f - qt = ~ / qt/Qt5.9.1
Copy the code

Static library program

Xmake uses the built-in build rule qt.static to apply it to target, which allows target to support qt static library construction

If you want to support other build environments, you just need to customize your own extension rules and apply them to the corresponding target.

target("test")
    add_rules("qt.static")
    add_files("src/*.cpp")
    add_frameworks("QtNetwork"."QtGui")
Copy the code

If you want to use some of Qt’s framework libraries, you can add add_frameworks to these frameworks, and the normal compilation process follows:

$ xmake
Copy the code

Dynamic library program

The dynamic library application is similar to the static library description rules described in the previous section, with the only difference being to change the build rule to add_rules(“qt.shared”).

target("test")
    add_rules("qt.shared")
    add_files("src/*.cpp")
    add_frameworks("QtNetwork"."QtGui")
Copy the code

How does add_rules(“qt.shared”) differ from set_kind(“shared”)?

  • set_kind("shared")Xmake: is the most basic dynamic library construction mode. It is very primitive and does not attach any framework layer dependent libraries and configuration
  • add_rules("qt.shared"): is only used to build the Qt dynamic library. It is a built-in extension rule that will attach the Qt SDK build environment

Console program

Similarly for the console, just replace the build rule: qt.console

target("test")
    add_rules("qt.console")
    add_files("src/*.cpp")
Copy the code

Quick application

From the latest Qt SDK, mainly provides two KINDS of UI APP construction framework, Quick App and Widgets App, Xmake also carry on support, and unified specification into: Qt. application Qt application rules to simplify the setup.

target("qt_quickapp")
    add_rules("qt.application")
    add_files("src/*.cpp") 
    add_files("src/qml.qrc")
    add_frameworks("QtQuick")
Copy the code

As described above, simply add the corresponding QML. QRC as the source file and attach the required QtQuick dependency library.

Note: Although xmake’s add_links is also used to add dependency libraries for linking, it is recommended to use add_frameworks for libraries provided by the Qt SDK, since all Qt build rules extend add_frameworks. Better support for Qt’s built-in framework libraries, and the ability to automatically switch debug/release versions of Qt libraries based on build mode.

Widgets applications

The description rules for Widgets App use the same qt.application, just add the. UI file to it. The only thing you need to be aware of is the header file with Q_OBJECTmeta, for example: Mainwindow. h, because there is a moC preprocessing process, it needs to be added to the source file so that Qt’s build rules can detect it and automatically preprocess it for MOC.

target("qt_widgetapp")
    add_rules("qt.application")
    add_files("src/*.cpp") 
    add_files("src/mainwindow.ui")
    add_files("src/mainwindow.h")  -- Add a meta header with Q_OBJECT
    add_frameworks("QtWidgets")
Copy the code

More details about Qt SDK environment support can be found at #160

Project source: github.com/tboox/xmake, welcome everyone star

tboox.org/cn/2018/05/…