Getting the subversion repository number into code


I'd like to implement a way of recording the version of a project within code, so that it can be used when testing and to help track bugs.
It seems the best version number to use would just be the current revision number from Subversion. Is there an easy way to hook this number into a (C++ in my case) header file or something, which I can then get at in code? I guess this is a post commit hook or something?

Does anyone have any experience of implementing this (with code to share, please?), or can suggest a better alternative?

Best Answer

Two ways:

Embed $Id$ or $Revision$ within the code. Then set svn:keywords="Id Revision" property on the file. This will give you the last modified revision of that source file. Good for smaller projects and scripts.

Alternatively, use a Makefile driven process and the command line tool svnversion. (Language specific - this should work for C/C++)

echo -n "#define VERSION 1.0.1-" > version.h
svnversion -n . >> version.h

Or some more complex build script with sed and Then just #include version.h

That will give you the repository version number, which will change with every commit / update, and is probably a more appropriate version number for most projects.

Note: I also used a human readable version string that I manually update. The example would give: Version: 1.0.1-r13445