Qbs Manual | How-tos

Qbs 1.12.2

Custom Modules and Items ◦ Reference

# How-tos

This page provides concrete instructions for common scenarios.

- How do I build a Qt-based project?
- How do I make my app build against my library?
- How do I use precompiled headers?
- How do I run my autotests?
- How do I create a module for a third-party library?
- How do I create application bundles and frameworks on iOS, macOS, tvOS, and watchOS?
- How do I apply C/C++ preprocessor macros to only a subset of the files in my product?
- How do I make the state of my Git repository available to my source files?

## How do I build a Qt-based project?

First of all, your project files need to declare dependencies on Qt modules.

To build the project, you need a matching profile. The following commands set up and use a Qt-specific profile:

      $ qbs setup-qt /usr/bin/qmake qt
      $ cd my_project
      $ qbs profile:qt

If you plan to use this profile a lot, consider making it the default one:

      $ qbs config defaultProfile qt
      $ cd my_project
      $ qbs

See Managing Qt Versions for more details.

Note: These instructions are only relevant for building from the command line. If you use Qt Creator, profiles are set up automatically from the information in the Kit.

## How do I make my app build against my library?

This is achieved by introducing a dependency between the two products using the Depends item. Here is a simple, but complete example:

      import qbs
      Project {
          CppApplication {
              name : "the-app"
              files : [ "main.cpp" ]
              Depends { name: "the-lib" }
          }
          DynamicLibrary {
              name: "the-lib"
              Depends { name: "cpp" }
              files: [
                  "lib.cpp",
                  "lib.h",
              ]
              Export {
                  Depends { name: "cpp" }
                  cpp.includePaths: [product.sourceDirectory]
             }
          }
      }

The product `the-lib` is a dynamic library. It expects other products to build against it, and for that purpose, it exports an include path (via an Export item), so that the source files in these products can include the library's header file.

The product `the-app` is an application that expresses its intent to link against `the-lib` by declaring a dependency on it. Now `main.cpp` can include `lib.h` (because of the exported include path) and the application binary will link against the library (because the linker rule in the cpp module considers library dependencies as inputs).

Note: In a non-trivial project, the two products would not be defined in the same file. Instead, you would put them into files of their own and use the Project.references property to pull them into the project. The product definitions would stay exactly the same. In particular, their location in the project tree is irrelevant to the relationship between them.

## How do I use precompiled headers?

If you use a Group item to add a precompiled header file to a product and mark it with the relevant file tag (`c_pch_src`, `cpp_pch_src`, `objc_pch_src`, or `objcpp_pch_src`), it is used automatically.

Only one precompiled header is allowed per product and language.

For example:

      import qbs

      CppApplication {
          name: "the-app"
          files: ["main.cpp"]

          Group {
              files: ["precompiled-header.pch"]
              fileTags: ["cpp_pch_src"]
          }
      }

## How do I run my autotests?

There are two simple things you need to do in your project. Firstly, you mark your test executables as such. This is done by adding the tag `"autotest"` to the product type:

      CppApplication {
          name: "test1"
          type: base.concat("autotest")
          // ...
      }

The second step is to instantiate an AutotestRunner product in your project:

      Project {
          // ...
          AutotestRunner { name: "run_my_tests" }
      }

Building an AutotestRunner product does not produce artifacts, but triggers execution of all applications whose products are tagged as autotests:

      $ qbs -p run_my_tests
      test1: PASS
      test2: PASS
      test3: FAIL
      ...

See the AutotestRunner documentation for how to fine-tune the behavior.

## How do I create a module for a third-party library?

If you have pre-built binary files in your source tree, you can create modules for them and then introduce dependencies between your project and the modules to pull in the functionality of a third-party library.

Create the following folder structure to store the module files:

      $projectroot/modules/ThirdParty

Then create a file in the directory that specifies the module properties for each supported toolchain. The filename must have the `.qbs` extension. The module will be pulled in if a product declares a dependency on it.

In the following example, `lib1.dylib` is a multi-architecture library containing both 32-bit and 64-bit code.

      ---ThirdParty.qbs---

      Module {
          Depends { name: "cpp" }
          cpp.includePaths: ["/somewhere/include"]
          Properties {
              condition: qbs.targetOS.contains("android")
              cpp.dynamicLibraries: ["/somewhere/android/" + Android.ndk.abi + "/lib1.so"]
          }
          Properties {
              condition: qbs.targetOS.contains("macos")
              cpp.dynamicLibraries: ["/somewhere/macos/lib1.dylib"]
          }
          Properties {
              condition: qbs.targetOS.contains("windows") && qbs.architecture === "x86"
              cpp.dynamicLibraries: ["/somewhere/windows_x86/lib1.lib"]
          }
          Properties {
              condition: qbs.targetOS.contains("windows") && qbs.architecture === "x86_64"
              cpp.dynamicLibraries: ["/somewhere/windows_x86_64/lib1.lib"]
          }
      }

Finally, declare dependencies on `ThirdParty` in your project:

      import qbs
      CppApplication {
          name: "the-app"
          files: ["main.cpp"]
          Depends { name: "ThirdParty" }
      }

## How do I create application bundles and frameworks on iOS, macOS, tvOS, and watchOS?

Creating an application bundle or framework is achieved by introducing a dependency on the bundle module and setting the bundle.isBundle property to `true`.

Here is a simple example for an application:

      import qbs

      Application {
          Depends { name: "cpp" }
          Depends { name: "bundle" }
          bundle.isBundle: true
          name: "the-app"
          files: ["main.cpp"]
      }

and for a framework:

      import qbs

      DynamicLibrary {
          Depends { name: "cpp" }
          Depends { name: "bundle" }
          bundle.isBundle: true
          name: "the-lib"
          files: ["lib.cpp", "lib.h"]
      }

Qbs also supports building static frameworks. You can create one by replacing the DynamicLibrary item with a StaticLibrary item in the example above.

Note: When using the Application item (or convenience items, such as CppApplication, DynamicLibrary, and StaticLibrary), your products will be built as bundles on Apple platforms by default (this behavior is subject to change in a future release).

To explicitly control whether your product is built as a bundle, set the `bundle.isBundle` property. Setting the consoleApplication property of your product will also influence whether your product is built as a bundle.

Building your application against your framework is the same as linking a normal dynamic or static library; see the How do I make my app build against my library? section for an example.

## How do I apply C/C++ preprocessor macros to only a subset of the files in my product?

Use a Group item to define a subset of project files. To add macros within the group, you need to use the `outer.concat` property, because you are adding macros to those specified in the outer scope.

In the following example, `MACRO_EVERYWHERE` is defined for all files in the Product unless a Group overrides the macro, whereas `MACRO_GROUP` is only defined for `groupFile.cpp`.

      Product {
          Depends { name: "cpp" }
          cpp.defines: ["MACRO_EVERYWHERE"]
          Group {
              cpp.defines: outer.concat("MACRO_GROUP")
              files: "groupFile.cpp"
          }
      }

The `cpp.defines` statements inside a `Group` only apply to the files in that `Group`, and therefore you cannot use a `Group` to include a bunch of files and globally visible macros. The macros must be specified in a Properties item at the same level as the `Group` if they need to be visible to files outside the `Group`:

      Product {
          Depends { name: "cpp" }
          Group {
              condition: project.supportMyFeature
              files: "myFile.cpp"
          }

          property stringList commonDefines: ["ONE", "TWO"]

          Properties {
              condition: project.supportMyFeature
              cpp.defines: commonDefines.concat("MYFEATURE_SUPPORTED")
          }
      }

## How do I make the state of my Git repository available to my source files?

Add a dependency to the vcs module to your product:

      CppApplication {
          // ...
          Depends { name: "vcs" }
          // ...
      }

Your source files will now have access to a macro whose value is a string representing the current Git or Subversion HEAD:

      #include <vcs-repo-state.h>
      #include <iostream>

      int main()
      {
          std::cout << "I was built from " << VCS_REPO_STATE << std::endl;
      }

This value is also available via the vcs.repoState property.

Custom Modules and Items ◦ Reference