Get version number at compile time

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

Parents
  • 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
    


    you then add a command to run a batch file from the 'Options->Output->(After Make)Run User Program #1'. That batch file would only contain the increment exe and the file to search, like this:
    c:\util\incbld .\version.h
    


    In my code I have a version printout that would look like this:
    printf("Version %s, build: %d\r\n", VERSION_STRING, (uint)BUILD_NUMBER);
    


    Here is the code to the incbld.exe program which should be able to be compiled by any dos compiler, (free ones too). This was never meant to go main stream, so use at your own risk!



    #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);
    }
    



    Hope it helps!

    David

Reply Children
No data
More questions in this forum