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!

One response to “Increase build number in C/C++ header (using Awk)

  1. Great script, thanks for that. If it helps anyone, when used inside a make script, escape the ‘$’ symbols with another ‘$’.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s