Auto generate build/version number
Hi,
You may, for example, create a file named makefile.targets at the root of your project, with the following content:all: ../build_number.h ../build.number: if [ ! -r ../build.number ]; then \ echo build_number=1 > ../build.number; \ fi bump.build.number: source ../build.number; \ echo "build_number=$$((build_number + 1))" > ../build.number .PHONY: bump.build.number post-build: bump.build.number ../build_number.h: ../build.number source ../build.number; \ echo "#define BUILD_NUMBER $$build_number" > $@Be careful: the backslashes at the end of some lines must really be the last character of the line, with no spaces after, or this will not work (you can also put everything on one line but it’s far less readable).
Then include build_number.h in the file in which you need the build number; this file will define a macro named BUILD_NUMBER which will be bumped at each successful build.
Beware that each time you will launch a build, even if nothing else has changed, the program will be updated as th ebuild number has changed... Only bumping the build number when something has effectively changed (and not for each successful build) will need quite a few more logic...
Also you may have to slightly edit this makefile fragment if you want the generated files elsewhere in your project (although makefile.targets must stay at the root of the project).
Note also that this will only work if you use the external builder (make), not the internal one.This is one of the advantages of the external builder.
Bernard (Ac6)