Issue
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?
Solution
You need a 2 step process:
- Add a Qt Creator build setting to create a CMake cache variable.
- In CMakeLists.txt use the cache variable within a command.
For your example:
- In Qt Creator build settings,
- either click Add > String setting Key to
MYPROJECT_FOO
and Value tobar
- or click Batch Edit... and add
-DMYPROJECT_FOO:STRING=bar
- 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.
Answered By - Paul Masri-Stone Answer Checked By - Cary Denson (WPSolving Admin)