Make a cmake command line variable for the compiler

I have the following code:

int main() {
#ifdef COMMIT_VERSION
   cout << "app version: " << COMMIT_VERSION << endl;
#endif
}

      

I would like to call cmake such that it will pass the COMMIT_VERSION variable defined on the command line to g ++ and thus to my application. For example. after calling:

cmake -WHAT_IS_THE_OPTION_NAME COMMIT_VERSION='"Hello Version"'
make
./a.out

      

outputs output

app version: Hello Version

      

+3


source to share


1 answer


You can use a parameter -D <var>:<type>=<value>

to add a definition to the cmake script ( type

and value

optional), for example:

cmake -D COMMIT_VERSION='"whatever version here"' ...

      



Then, inside the script, you can use a function add_definitions

to pass the definition to g ++:

add_definitions(-DCOMMIT_VERSION=${COMMIT_VERSION})

      

+8


source







All Articles