c++qtcmakeqt-creatorbuild-settings

Custom values in Qt Creator (CMake) build settings


I want to achieve the equivalent of C++ #define foo "bar" but I don't want the value "bar" to be part of my code repository, and the value will depend on whether the build is Debug or Release.

How can I do this?

NB: I like the idea of using the following in CMakeLists.txt add_compile_definitions(foo="bar") but I don't know how to supply "bar" from the Qt Creator build settings.

Presumably I'd add a Key/Value pair but what would I put?

Qt Creator build settings


Solution

  • You need a 2 step process:

    1. Add a Qt Creator build setting to create a CMake cache variable.
    2. In CMakeLists.txt use the cache variable within a command.

    For your example:

    1. In Qt Creator build settings,
    1. In CMakeLists,

    target_compile_definitions(mytarget PUBLIC foo="${MYPROJECT_FOO}")

    (As advised by Guillaume Racicot it's better to apply the definition to a single target, as opposed to the entire project, which would be add_compile_definitions(foo="${MYPROJECT_FOO}").)

    See also Qt Creator documentation CMake Build Configuration: Modifying Variable Values.

    Thanks to Guillaume Racicot for deeper explanation of how CMake cache variables can be used.