Click here to Skip to main content
15,880,651 members
Articles / Programming Languages / C
Article

Add Build Date and Autoincrement Build Number

Rate me:
Please Sign up or sign in to vote.
4.12/5 (15 votes)
24 May 20053 min read 88.4K   2.1K   61   10
This article is about adding automation to your project that increments the build number and adds a build date that can be seen in the Version tab in the file properties window.

Introduction

There are many articles and applications posted here, but I couldn't find one that could do what I wanted. I don't want to add any plug-in to Visual Studio, or modify any .rc files that the IDE should maintain and modify. I want to add more version info like build date. Some of the solutions given here are good but are not complete. They represent only a starting point to my solution.

Solution

The goal is to add dynamic version numbers in the .rc file of your project and a build a date string. Starting from the point that the .rc file of your project supports #include directives, I made the following:

  1. Defined some constants:
    #define VERSION_MAJOR 4
    #define VERSION_MINOR 5
    #define VERSION_BUILD 46
    #define VERSION_QFE 11
    #define VERSION_BUILD_DATE "24/05/2005"
    #define VERSION_BUILD_TIME "08:37:55UTC"
    
    #define _STR(x) #x
    #define STR(x) _STR(x)
    #define VERSION_NUMBER VERSION_MAJOR,VERSION_MINOR,
                                      VERSION_BUILD,VERSION_QFE
    #define VERSION_STRING STR(VERSION_MAJOR) "." STR(VERSION_MINOR) "." \
                                   STR(VERSION_BUILD) "." STR(VERSION_QFE)
    
    #define VERSION_COMPANY ""
    #define VERSION_COPYRIGHT "(C) Gugulea 2005"
    #define VERSION_TRADEMARK ""
    #define VERSION_BUILD_DATE_TIME VERSION_BUILD_DATE " - " \
                                                     VERSION_BUILD_TIME

    The version of a file looks like this: 4.5.46.11. The numbers represent the major, minor, build and qfe numbers of your product. I chose to modify the build number, though my solution easily supports modifications of any of the numbers and even adds more.

  2. Put all these #defines in a version.h file, which I'll include in the .rc file:
    #ifndef _MAC
    /////////////////////////
    //
    // Version
    //
    #include "version.h"
    .........................
    #endif    // !_MAC

    because only some of these numbers/strings are modified, I have separated them into version.ver:

    #define VERSION_MAJOR 4
    #define VERSION_MINOR 5
    #define VERSION_BUILD 46
    #define VERSION_QFE 11
    #define VERSION_BUILD_DATE "24/05/2005"
    #define VERSION_BUILD_TIME "08:37:55UTC"

    and version.h:

    #include "version.ver"
    
    #define _STR(x) #x
    #define STR(x) _STR(x)
    
    #define VERSION_NUMBER VERSION_MAJOR,VERSION_MINOR,
                                  VERSION_BUILD,VERSION_QFE
    #define VERSION_STRING STR(VERSION_MAJOR) "." STR(VERSION_MINOR) "." \
                                     STR(VERSION_BUILD) "." STR(VERSION_QFE)
    #define VERSION_COMPANY ""
    #define VERSION_COPYRIGHT "(C) Gugulea 2005"
    #define VERSION_TRADEMARK ""
    #define VERSION_BUILD_DATE_TIME VERSION_BUILD_DATE " - " \
                                              VERSION_BUILD_TIME

    version.h includes version.ver and is included by the .rc file, therefore instead of adding the version numbers directly into the .rc files I have separated them into more files. The only file that will be modified is the version.ver file, which is pretty simple and easy to maintain and modify, and it will be the only one that will be automatically modified on each build.

    The .rc file will look like this:

    #ifndef _MAC
    //////////////////////////////////////////
    //
    // Version
    //
    #include "version.h"
    
    VS_VERSION_INFO VERSIONINFO
    FILEVERSION VERSION_NUMBER
    PRODUCTVERSION VERSION_NUMBER
    ...
    VALUE "Build Date", VERSION_BUILD_DATE_TIME "\0"
    VALUE "CompanyName", VERSION_COMPANY "\0"
    VALUE "FileVersion", VERSION_STRING "\0"
    VALUE "LegalCopyright", VERSION_COPYRIGHT "\0"
    VALUE "LegalTrademarks", VERSION_TRADEMARK "\0"
    VALUE "ProductVersion", VERSION_STRING "\0"
    ...
    #endif    // !_MAC

    The "Build Date" string is added manually and will appear in the version tab of the module, DLL or EXE.

    Now there is another problem with Visual C++ and .rc files. If you modify some of the resources or add new resources to your application it will overwrite the version strings. Therefore you can put all the previous part in a .rc2 file, which you can include in the .rc file.

    That's all, the two files that you have included in the resource files are: version.h and version.ver. The last one will be modified by an external application, whereas the resource files are not touched. The file makeversion.exe will update the VERSION_BUILD_DATE and the VERSION_BUILD_TIME values to the current ones and will increment the VERSION_BUILD number. You can modify it to update any of the numbers in version.ver, like for example VERSION_MINOR.

  3. Run the makeversion tool at build time. For VC6 you have to add it in the "Pre-link step" entry of your project.
    For Release:
    makeversion.exe /inc version.ver
    rc.exe /l 0x409 /fo"Release/cdcdcd.res" /d "NDEBUG" "C:\cdcdcd.rc"
    
    For Debug:
    makeversion.exe version.ver
    rc.exe /l 0x409 /fo"Debug/cdcdcd.res" /d "_DEBUG" "C:\cdcdcd.rc"

    The rc command can be found in the Build log of the project.

The /inc option will increment the build number and should only be added in the release version of your product. makeversion.c is written in C; version.ver file contains the standard #define directives and doesn't support any comments; empty lines are also ignored; /h or /? options in the command line shows more parameters. If version.ver file does not exist it will be automatically created. If it is empty it will be filled with default version 1.0.0.0 and the current time and date.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalthis might be of interest Pin
dB.26-Oct-08 7:43
dB.26-Oct-08 7:43 
GeneralKB Q237870 Pin
p e r t h e l i12-Sep-06 15:32
p e r t h e l i12-Sep-06 15:32 
A similar article:
Microsoft Knowledge Base article Q237870
"How to increment version information after each build in Visual C++"
http://support.microsoft.com/kb/q237870/
GeneralRe: KB Q237870 Pin
2pers2-Feb-07 6:28
2pers2-Feb-07 6:28 
Generalthe version numbers Pin
gugulea14-Jun-06 20:57
gugulea14-Jun-06 20:57 
GeneralGood Tool Pin
月夜精林7-Jun-06 18:27
月夜精林7-Jun-06 18:27 
GeneralNice Pin
tworca30-Nov-05 8:22
tworca30-Nov-05 8:22 
QuestionCan we have five level of version number in File Version in properties... ??? Pin
Jigar Mehta13-Jun-05 19:51
Jigar Mehta13-Jun-05 19:51 
AnswerRe: Can we have five level of version number in File Version in properties... ??? Pin
gugulea13-Jun-05 20:39
gugulea13-Jun-05 20:39 
GeneralSource Safe Auto-Chekout Pin
Wanderley Caloni31-May-05 6:06
professionalWanderley Caloni31-May-05 6:06 
GeneralRe: Source Safe Auto-Chekout Pin
Ulfen31-May-05 20:17
Ulfen31-May-05 20:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.