3

I have C++ project using cmake. I'd like to have versioning (in meaning of provide unique ./myapp --version output) based on git commits. Manually it's something like

gcc ... -DVERSION=$(git rev-list|wc -l)

and using VERSION macro if defined in code.

This works as expected. But how should I write it into CMakeLists.txt (let's use echo for simplicity)? How should I escape it?

add_definitions(-DVERSION="$(echo 1)")
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Miso
  • 161
  • 2
  • 11
  • Similar, but not identical to [this question](http://stackoverflow.com/questions/1435953/how-can-i-pass-git-sha1-to-compiler-as-definition-using-cmake) – Fraser Jun 19 '12 at 09:58

1 Answers1

1

In cmake 2.8.8 you can:

add_definitions(demo -D`echo VERSION=\\`git rev-list HEAD|wc -l\\``)

and:

int main(int argc, char *argv[]) {
  printf("Version: %d", VERSION);

To compile as:

/usr/bin/gcc   -std=c99 demo -D`echo VERSION=\`git rev-list HEAD|wc -l\`` -o CMakeFiles/demo.dir/src/demo.c.o   -c /home/doug/projects/libar/src/demo.c

And results in:

build$ ./demo
Version: 88

This seems to be what you actually want.

Doug
  • 32,844
  • 38
  • 166
  • 222