<p>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?</p>

<p>Does anyone have any experience of implementing this (with code to share, please?), or can suggest a better alternative?
Thanks.</p>

[{'answer_id': 16264, 'author': 'Vincent Robert', 'author_id': 268, 'author_profile': 'https://Stackoverflow.com/users/268', 'pm_score': 3, 'selected': False, 'text': '<p>You can use the <code>svn:keywords</code> property to enable the <code>Rev</code> keyword. </p>\n\n<p>You can then use <code>$Rev$</code> in your code and SVN will expand it automatically when updating to <code>$Rev: 256 $</code> which can then parse...</p>\n\n<p><a href="http://svnbook.red-bean.com/en/1.0/ch07s02.html#svn-ch-7-sect-2.3.4" rel="noreferrer">More info on the Subversion manual</a></p>\n'}, {'answer_id': 16276, 'author': 'jmanning2k', 'author_id': 1480, 'author_profile': 'https://Stackoverflow.com/users/1480', 'pm_score': 5, 'selected': False, 'text': '<p>Two ways:</p>\n\n<p>Embed <em>$Id$</em> or <em>$Revision$</em> within the code. Then set <em>svn:keywords="Id Revision"</em> property on the file. This will give you the last modified revision of that source file. Good for smaller projects and scripts.</p>\n\n<p>Alternatively, use a Makefile driven process and the command line tool svnversion. (Language specific - this should work for C/C++)</p>\n\n<pre><code>echo -n "#define VERSION 1.0.1-" &gt; version.h\nsvnversion -n . &gt;&gt; version.h\n</code></pre>\n\n<p>Or some more complex build script with sed and version.h.in. Then just <em>#include version.h</em></p>\n\n<p>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.</p>\n\n<p>Note: I also used a human readable version string that I manually update. The example would give: <strong>Version: 1.0.1-r13445</strong> </p>\n\n<p>~J</p>\n'}, {'answer_id': 35683, 'author': 'Rob', 'author_id': 3542, 'author_profile': 'https://Stackoverflow.com/users/3542', 'pm_score': 5, 'selected': True, 'text': '<p>While nifty, the revision keyword trick only updates the file when it\'s changed in that revision - if you don\'t change the file, then it will continue to reflect the old revision.</p>\n\n<p>If you want the software to always reflect the overall revision number, then you\'ll have to delve into the <a href="http://svnbook.red-bean.com/en/1.1/ch08s03.html" rel="noreferrer">relevant SVN entries file</a> and extract it, which isn\'t too difficult (it\'s an XML file).</p>\n\n<p>Wikipedia does this on <a href="http://en.wikipedia.org/wiki/Special:Version" rel="noreferrer">their version page</a> to indicate the revision of the software that\'s running live; <a href="http://svn.wikimedia.org/svnroot/mediawiki/trunk/phase3/includes/specials/SpecialVersion.php" rel="noreferrer">the code is here</a> - look for the <strong>getSvnRevision()</strong> method.</p>\n'}, {'answer_id': 186646, 'author': 'Bill', 'author_id': 26449, 'author_profile': 'https://Stackoverflow.com/users/26449', 'pm_score': 4, 'selected': False, 'text': '<p>You can also use SubWCRev which is part of TortoiseSVN.</p>\n\n<p><em>SubWCRev is Windows console program which can be used to read the status of a Subversion working copy and optionally perform keyword substitution in a template file. This is often used as part of the build process as a means of incorporating working copy information into the object you are building. Typically it might be used to include the revision number in an “About” box.</em></p>\n\n<p><a href="http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html" rel="noreferrer">http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html</a></p>\n'}, {'answer_id': 16492697, 'author': 'iCoder', 'author_id': 666953, 'author_profile': 'https://Stackoverflow.com/users/666953', 'pm_score': 3, 'selected': False, 'text': '<p>in your Makefile, add:</p>\n\n<pre><code>SVNDEV := -D\'SVN_REV="$(shell svnversion -n .)"\'\nCFLAGS := $(SVNDEV) ...\n</code></pre>\n\n<p>then you can use macro <code>SVN_REV</code> anywhere in your code, eg:</p>\n\n<pre><code>printf ("Version: SVN %s\\n", SVN_REV);\n</code></pre>\n'}, {'answer_id': 43468392, 'author': 'Jesse', 'author_id': 7603292, 'author_profile': 'https://Stackoverflow.com/users/7603292', 'pm_score': 0, 'selected': False, 'text': '<p>A good up-to-date solution:</p>\n\n<p>Create a <code>Makefile</code> containing the following line (in the same folder as <code>YourFile.dox</code>):</p>\n\n<pre><code>sed "s~RevNumber~$(shell svnversion ../)~g" YourFile.dox &gt; YourFileDummy.dox; doxygen YourFileDummy.dox\n</code></pre>\n\n<p>And <code>YourFile.dox</code> should contain this:</p>\n\n<pre><code>...\nPROJECT_NUMBER         = "Revision RevNumber"\n...\n</code></pre>\n\n<p>Now:</p>\n\n<ol>\n<li><code>sed</code> replaces <code>RevNumber</code> in the .dox with the output of <code>svnversion</code> (executed in the main folder of your repository) and saves the modified file to <code>YourFileDummy.dox</code></li>\n<li><code>doxygen</code> is executed on <code>YourFileDummy.dox</code> to generate the documentation</li>\n<li>Your documentation will now contain the revision number!</li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/16248', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1169/']