We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Is it possible to have the version number of a compile auto increment and be a part of the code to be retrieved at run time? Thanks
No. The nearest you get with the Keil tools alone is __DATE__ and __TIME__. You would need a 3rd-party (or homegrown) utility to provide such a build number.
Sean, I quickly put together a dos program some time ago to increment what I call a 'build' number. I have used it for years, it may not be the most robust, but works for me. Im sure that there are other ways, but like you, I was looking for something simple to do just what you wanted. What the program does, is looks for a string in a file and increments the number following by 1. In this case I have "BUILD_NUMBER" in my version.h file like this:
#define BUILD_NUMBER 321
c:\util\incbld .\version.h
printf("Version %s, build: %d\r\n", VERSION_STRING, (uint)BUILD_NUMBER);
#include <stdio.h> #include <string.h> #define SEARCH_STRING "BUILD_NUMBER" main(int argc, char *argv[]) { FILE *fp; unsigned int i, number; char str[128], str2[64]; char *foundstr; int temp; if(argc <= 1) { printf("Increment Build number, Ver: 1.0 by: D. Marten \n"); printf(" Usage: incbld <filename) \n"); printf(" where filename is name of file to search for string\n"); printf(" BUILD_NUMBER (ususally contained in a headder file. \n"); printf(" The number following will be incremented by 1 and saved. \n\n\n"); exit(1); } fp=fopen(argv[1], "r+"); if(fp==NULL) { printf("error opening file %s. \n", argv[1]); exit(1); } *str = 0; while(!feof(fp)) { if((foundstr=strstr(str, SEARCH_STRING)) != NULL) /* if string found */ { sscanf(foundstr, "%*s %s", str2); number = atoi(str2); number++; temp = ftell(fp) - strlen(foundstr)+strlen(SEARCH_STRING) ; fseek(fp, (long)temp, SEEK_SET); fprintf(fp," %-4d\n", (int)number); fflush(fp); fseek(fp, 0L, SEEK_END); } fgets(str, 127, fp); } fclose(fp); return(0); }
Rather than #define BUILD_NUMBER directly in the source code, which requires modifying the source file for each build, just define the preprocessor symbol on the compiler command line (or uVision options panel). No need to overwrite the file each time. You do, however, have the same problem with updating the number used in your build environment. Again, it's usually better to set an environment variable or pass the option into the build program than to physically modify a hard-coded value. For day-to-day usage, I usually go by the date/time of the build, and save incrementing a build number for "official" releases (whatever those may be in your process; maybe as often as daily).
Im not sure if I follow how the #define BUILD_NUMBER would change for each build. The increment program was created once years ago and used for multiple projects. No need to change it, as long as you have the string "BUILD_NUMBER" somewhere in the referenced file (in my case, 'version.h'). There is no recompiling of the DOS program. I agree though that the string to modify should actually be passed though the command line, but for me, I have found that it is always the same string anyway! Im always open to a better version!
I described by timestamp.c approach in this thread (it's near the end): http://www.keil.com/forum/docs/thread2789.asp
Excellent!!!! It works great and is the perfect small solution I was looking for. Thanks David. Sean
Im not sure if I follow how the #define BUILD_NUMBER would change for each build. Use the compiler command line option to define the value of BUILD_NUMBER. Instead of
file.c: // edit xxx to build number for each release #define BUILD_NUMBER xxx ... printf ("Build: %u\n", BUILD_NUMBER); Make: build_number_patch file.c 123 c51 file.c
file.c: // no #define in this file printf ("Build: %u\n", BUILD_NUMBER); Make: c51 DEFINE (BUILD_NUMBER=$BUILD) file.c