Automatic increment of build number in Qt Creator


Question

I would like to have a variable (or #define) in C++ source that will increment each time I use Qt Creator to build source code. Is there any way I can do this, perhaps some Qt Creator plugin or similar? If there is a way to do it if I use "make" on command line to build?

1
19
11/26/2012 10:32:45 PM

Accepted Answer

In your .pro file, you can create a variable that contains the results of a command-line program. You can then use that to create a define.

BUILDNO = $$(command_to_get_the_build_number)
DEFINES += BUILD=$${BUILDNO}

If you just want a simple incrementing number, you could use a pretty simple script:

#!/bin/bash
number=`cat build_number`
let number += 1
echo "$number" | tee build_number #<-- output and save the number back to file

I should note that this would cause the build number to increment every time you build, and also increment if you try to build but it fails. A better way is to get a build number based on the state of the code, and many version control tools can get you a text string for that, if not a number.

18
5/8/2011 5:29:59 PM

The Windows equivalent for Joerg Beutel's improved solution https://stackoverflow.com/a/5967447/1619432:

.pro:

build_nr.commands = build_inc.bat
build_nr.depends = FORCE
QMAKE_EXTRA_TARGETS += build_nr
PRE_TARGETDEPS += build_nr

HEADERS  += build.h

build_inc.bat:

@echo off 
set /p var= <build.txt 
set /a var= %var%+1 
echo %var% >build.txt
echo #define BUILD %var% >build.h
echo %var%

Usage

#include "build.h"
...
qDebug() << "Build number:" << BUILD;

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon