7

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 
3
  • You probably want the configure_file function. Commented Aug 8, 2014 at 16:05
  • Due to quite convoluted build system. I would prefer not to use files just command line argument passing. Commented Aug 8, 2014 at 16:08
  • Perhaps related (or a duplicate): stackoverflow.com/q/7900661/417197 Commented Aug 11, 2014 at 5:51

1 Answer 1

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}) 
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.