Increase build number in C/C++ header (using Awk)

This trick works with most IDE’s that use pre-build step, but easiest I guess is to use it in a Makefile. Feel free to modify the “script” in any way it suites your purposes.

Suppose you have a build number defined as #define BuildNum 1 in BuildNum.h header.

All you have to do is add the following command in your make all section in Makefile ahead of other dependencies (it’s one line, broken here for visibility):

awk '$2 !~ /BuildNum/ {print} $2 ~ /BuildNum/ {print "#define BuildNum "$3+1}'\
    BuildNum.h > BuildNum.h~; mv BuildNum.h~ BuildNum.h

Yes, it’s a clumsy solution, but it does the work just fine… 😀 Sure, you can catch the lines in array, and afterwards print them one by one back to file, etc. … but it’s just not at all pretty bash script (enhancements and suggestions welcome!).

Notice that the command is pretty much dependent on the format of the line the BuildNum is defined on, and that “BuildNum” should not appear anywhere else as a second word on the line!