Blog

2011-06-09 19:04:32

Auto-Incrementing Build Counts in Visual Studio C++

Presented here is the quick (and dirty) method I use to auto-increment the build numbers of the projects I work on. I initially wrote a very small program (prebuild.exe) which takes a single command line parameter as input. This parameter is the name of a header file which is included in a project.

The program reads in the contents of the header file, increments the value of the system_version_build #define and saves the file back to storage. The structure of the header file is very specific - any deviation from its structure will results in prebuild.exe failing.

A pre-build event is added to the project in question, which calls prebuild.exe, passing in the filename of the header file to update:

The command line used in the pre-build event is as follows:

1
c:\usr\fusi\dev\prebuild\prebuild.exe build_count.h

And here is what the contents of build_count.h looks like:

1
2
3
4
5
6
#ifndef build_count_h
#define build_count_h

#define system_version_build 265

#endif

This header file is accompanied by two other source files to be included in the project; buid.cpp and build.h. These exist to isolate the modification of build_count.h from the rest of the project code base. build.cpp includes build_count.h, and build.h exposes the interface to query the value of the system_version_build definition. As a result, only build.cpp requires recompilcation (as it is the only translation unit which includes build_count.h) - this is not a concern as build.cpp is very small and compiles very quickly.

Displayed below is the contents of build.h - the VERSION definitions are used by the resource file associated with the project so that the version number can be displayed in the details pane of the file properties dialog in windows explorer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// $Header: /project/build.h 1     7/01/12 4:14 fusi $

#ifndef build_h
#define build_h

#include "build_count.h"

#define VERSION_MAJOR               1
#define VERSION_MINOR               0
#define VERSION_REVISION            0
#define VERSION_BUILD               system_version_build

#define VERSION_BUILD_DATE __TIMESTAMP__

namespace version
{
    uint buildCount();
    std::string buildDate();
    uint major();
    uint minor();
    std::string asString();
}

#endif

The contents of build.cpp is shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// $Header: /project/build.cpp 2     7/01/12 7:41 fusi $

#include "prerequisites.h"
#include "build.h"

namespace version
{
    uint buildCount()
    {
        return system_version_build;
    }

    std::string buildDate()
    {
        return VERSION_BUILD_DATE;
    }

    uint major()
    {
        return VERSION_MAJOR;
    }

    uint minor()
    {
        return VERSION_MINOR;
    }

    std::string asString()
    {
        return util::tostr(major(), 1) + "." + util::tostr(minor(), 2) + "." + util::tostr(buildCount());
    }
}

This is what the output looks like during a build:

I hope you find this useful! All of the files required to make this work for your projects are available for download below:

prebuild.zip

Size: 205.34 KB, Date: 2012-01-12 19:43, MD5: d527157b54c5101370d28014ca5f4f3c