1) Create an .exe file from this code (say update_bn.exe):
Code: Select all
#include <stdio.h>
FILE *stream, *stream2;
int main( void )
{
char s[22];
unsigned long i;
if( !(stream = fopen("version.h", "rw")) )
printf( "The file version.h was not opened.\n" );
else
{
// Read data back from file:
fscanf( stream, "%s", s, 22 );
fscanf( stream, "%s", s, 22 );
fscanf( stream, "%ul", &i, 5 );
fclose( stream );
stream2 = fopen("version.h", "w");
//printf( "Build number %u -> %u\n", i, i+1 );
fprintf(stream2, "#define BUILD_NUMBER %u\n", i+1);
fclose( stream2 );
}
return 0;
}Code: Select all
#define BUILD_NUMBER 13) In Visual Studio 2005, you right click your project (not the solution), select Properties. Under Configuration Properties->Build Events->Pre-Build Events you insert 'update_bn.exe' to Command Line.
4) Include the version.h in your project.
Now, every time you build the project, this exe-file will be executed prior to the build, and it will then increment the number in version.h. You now have access to BUILD_NUMBER that provides you the actual build number.
Note that it is a good idea to include version.h in a separate file (like version.cpp) that contains nothing but this include. The reason being that version.h is new to the compiler all the time, and if it is included in the main header file, you probably will need to compile ALL files every time (sic!
If you want the compiler to show the build number every time, un-comment the printf line.
In my current project I show it in the upper right corner, like this

You can probably make this thing work in other environments as well.
Happy building
[/code]