6

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
Trismegistos
  • 3,821
  • 2
  • 24
  • 41

1 Answers1

14

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})
Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
  • Using this solution I would have to provide COMMIT_VERSION to cmake as I previously wanted. It turned out that it would be more suitable if I could provide COMMIT_VERSION to make instead of cmake. Is it possible with cmake? – Trismegistos Aug 11 '14 at 09:31