Automatic build number for Visual Studio 2005 projects

A forum to store posts deemed exceptionally wise and useful
Post Reply
alc
Posts: 31
Joined: Sun Jun 25, 2006 10:59 pm
Location: Denmark

Automatic build number for Visual Studio 2005 projects

Post by alc »

This describes how to get an automatic build number (increments everytime oút compile (build)). It's not specifically for Irrlicht, but that's where I use it, so here goes ...

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;
}
2) Make a file version.h containing one line:

Code: Select all

#define BUILD_NUMBER 1

3) 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! :cry: ), rather than you version.cpp.

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

Image

You can probably make this thing work in other environments as well.

Happy building :)
[/code]
Post Reply