Version Numbers in a project with Qt

oggmonster picture oggmonster · Nov 9, 2012 · Viewed 8.4k times · Source

Version numbers are needed all over a project; in installers, code, toolchains etc. I despise duplication. I want my version numbers to be stored in one central authoritative location.

I am working with C/C++ and using Qt on various platforms. In Qt, qmake projects specify version numbers like:

VERSION = 1.2.3

In code I use something like in a header like Version.h:

#define VERSION_MAJ 1
#define VERSION_MIN 2
#define VERSION_REV 3
#define VERSION_STRING \"VERSION_MAJ\" "." \"VERSION_MIN\" "." \"VERSION_REV\"

My installer toolchain has support for C preprocessing so I can use the same version specified in Version.h. However, I don't know how to get qmake to use the same version number. I thought I could preprocess the pro file, but that won't work as # characters mean a comment in pro files and the C preprocessor will fall over.

Anyone know of a good way to keep my version number centralised?

Answer

MichK picture MichK · Nov 10, 2012

I use something like this in my build system

#.pro file
#Application version
VERSION_MAJOR = 1
VERSION_MINOR = 0
VERSION_BUILD = 0

DEFINES += "VERSION_MAJOR=$$VERSION_MAJOR"\
       "VERSION_MINOR=$$VERSION_MINOR"\
       "VERSION_BUILD=$$VERSION_BUILD"

#Target version
VERSION = $${VERSION_MAJOR}.$${VERSION_MINOR}.$${VERSION_BUILD}

And after that you can use VERSION_MAJOR and others as normal macro in your application.