When I deploy embedded apps in Keil IDE, it’s essential to update and know the current build version in my application, how can we make Keil IDE automatically generate the build version of the application each time we make a new build?
My solution for Keil IDE is using time and date plus two fixed major and minor values for generating a unique version string based on time.
void get_compiled_version(uint8_t major,uint8_t minor,uint8_t* version)
{
const char months[][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
char *CompileTime = __TIME__;
char *CompileDate = __DATE__;
version[0] = (CompileTime[6] - '0') * 10 + (CompileTime[7] - '0');//second
version[1] = (CompileTime[3] - '0') * 10 + (CompileTime[4] - '0');//minute
version[2] = (CompileTime[0] - '0') * 10 + (CompileTime[1] - '0');//hour
version[3] = (CompileDate[4] - '0') * 10 + (CompileDate[5] - '0');//day
version[5] = (CompileDate[9] - '0') * 10 + (CompileDate[10] - '0');//year
uint8_t month = 0;
for (int i = 0; i < 12; ++i)
{
if (strncmp(CompileDate, months[i], 3) == 0)
{
month = i + 1; // Months are 1-based
break;
}
}
version[4]=month;//month
version[6]=minor;
version[7]=major;
}
this will return an array that contains 8 bytes of the date and time of the last build.
if we print them in a string, we will have something like this:
printf("%d.%d.%2d%2d%2d%2d%2d%2d,ver[7],ver[6],ver[5],ver[4],ver[3],ver[2],ver[1],ver[0]);
Version: 0.1.240711075236
0 and 1 are major and minor parts of the version which you can feed them into the function
this value will change on every build and will be unique