I have following code:
int main() {
#ifdef COMMIT_VERSION
cout << "app version: " << COMMIT_VERSION << endl;
#endif
}
I would like to invoke cmake such that it passes COMMIT_VERSION variable defined on command line to g++ and thus to my application. E.g. following invocation:
cmake -WHAT_IS_THE_OPTION_NAME COMMIT_VERSION='"Hello Version"'
make
./a.out
produces output
app version: Hello Version
Modify your cache variable to a boolean if, in fact, your option is boolean. If you create the cache variable in the CMakeLists. txt file and then pass the argument via calling cmake, won't the CMakeList.
Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.
You can use the -D <var>:<type>=<value>
option to add a definition within the cmake script (type
and value
being optional), like so:
cmake -D COMMIT_VERSION='"whatever version here"' ...
Then, inside the script, you can use the add_definitions
function to pass the definition to g++:
add_definitions(-DCOMMIT_VERSION=${COMMIT_VERSION})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With