Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message

PostPosted: Sat Aug 20, 2005 4:43 pm   Post subject: Build/Versioning

One thing that I like to keep track of for changelogs and such is the build count of a particular program, and the version of said build. I also use the version information to keep track of which version of the executable I'm currently running (it's sucks when you're trying to solve a bug and not using the new executable to test... makes fixing the bug difficult at best)

Now... version isn't so hard, just create a header with the version and inlude it in the project. Build count is something more of a challenge.

I use MSVC++.Net to do my programming so this is focused mainly on that, however with very little work I'm sure you could get this to work on any platform.

first things first; auto incrementing a build count.
Since there is no built in function to do this I wrote my own (sorry wtd... it's a cludge...) short program to increment the build count, and then build a header containing the version and build count.


// AutoBuildCount.cpp : Defines the entry point for the console application.

#include <fstream>
#include <string>

int main(int argc, char* argv[])
        int build_count;
        std::ifstream build_in("build_count.txt");            // open the file "build_count.txt"
        build_in >> build_count;                                // get the number

        build_count++;            // increase the number
                                                                        // save back to the file
        std::ofstream build_out("build_count.txt");
        build_out << build_count << std::endl;
        std::ifstream version_in("build_version.txt");    // open "build_version.txt"
        std::string build_version;
        version_in >> build_version;                // read version

        std::ofstream header_out("Source\\Build_Version.h");    // open Source/Build_Version.h
        header_out << "#DEFINE PROJECT_VERSION \"" << build_version << "\"" << std::endl;       // output #DEFINE PROJECT_VERSION="version"
        header_out << "#DEFINE PROJECT_BUILD \"" << build_count << "\"" << std::endl;      // output #DEFINE PROJECT_BUILD="build"
        header_out << "#DEFINE PROJECT_BUILDVERSION "
                << "\"" << build_version << "-" << build_count << "\""
                << std::endl;                                                   // output #DEFINE PROJECT_BUILDVERSION=PROJECT_VERSION+"-"+PROJECT_BUILD

        return 0;

I wrote this in er... 5 minutes, tops? So it's not hte cleanest but it works.

Next: create two files in hte same directory as the exe; build_count.txt with contents of 0 (initial build count) and build_version.txt with whatever string of text you want as your version. Build hte above code and put the exe in the base directory... now here's the interesting part... I have all my code in a directory called Source off of the main directory (which contains my changelog, todo, makefile (or project), etc.). If you don't you may need to change the program above.

Now... you should have two files (build_count.txt, build_version.txt) and an exe in your base directory, and a source directory where a file called Build_Version.h will be created when the exe runs. Right now all this does is create the new header when you run the exe not what we want, but close.

Now all that's left is addign a custom build step that runs the exe; this I'll leave up to you to figure out as it's not so hard (and if you're actually trying this you probably know what your doing... mostly...)

**** NOTES ****
1. I've had very little sleep in the last few days; so this might seem a bit random
2. I wrote this and the accompanying code in < 10 minutes, so I can't guaruntee anythign about either
3. I thought that since I wanted such a feature other's might too, so I thought I'd share how I did it... that's not to say that it's something you should do.


PostPosted: Mon May 08, 2006 4:10 am   Post subject: (No subject)

another option is using ANT ( tools, create some task to update the version information. ANT tool is very handy for building large systems that consists of many projects.

PostPosted: Mon May 08, 2006 7:53 am   Post subject: (No subject)

Or you could just put something similar to this in your make file... Since this is such a small program it'd be much easier to use then learning how ANT works; especially for smaller projects.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to: