cancel
Showing results for 
Search instead for 
Did you mean: 

How to store compilation date in the final binary file in order to read it by an external program?

SegmentFault
Associate II

I am trying to migrate a project from IAR to STM32CubeIDE. In this project, I have two lines of code in the main (as global variables) that store the compilation date in the final binary file. These two lines are the following:

char const compilation_date[] ={'<','c','>',__DATE__[0],__DATE__[1],__DATE__[2],__DATE__[3],__DATE__[4],__DATE__[5],__DATE__[6],__DATE__[7],__DATE__[8],__DATE__[9],__DATE__[10],'<','/','c','>'};
 
#pragma required=compilation_date

The first line builds the compilation string date while the second tells to IAR compiler that, although the variable is not reference in the code, it must include it in the final binary.

To migrate it to STM32CubeIDE (that uses gcc compiler) I need to use __attribute__((used)) instead of #pragma required=compilation_date. So, the ported lines of code are

char const compilation_month[] __attribute__((used)) ={'<','c','>',__DATE__[0],__DATE__[1],__DATE__[2],__DATE__[3],__DATE__[4],__DATE__[5],__DATE__[6],__DATE__[7],__DATE__[8],__DATE__[9],__DATE__[10],'<','/','c','>'};

The problem is that when I compile the program I get the following error:

../Core/Src/main.c: error: initializer element is not constant

The reason is that gcc expects that global variables store constant values instead of values determined in compilation time.

So, the question is: how can I store the compilation date in the final binary file?

P.S: My objective is that I could read the compilation date reading the binary (using windows or linux)

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III
__attribute__((used))
static char compilation_date[] = "<c>" __DATE__ "</c>";

View solution in original post

3 REPLIES 3
Pavel A.
Evangelist III
__attribute__((used))
static char compilation_date[] = "<c>" __DATE__ "</c>";

It works!!. I try to remove the "static" keyword and it still works. So the trick is use __DATE__ instead __DATE__[0], __DATE__[1]... Could you explain why it works using __DATE__ (instead of accessing to its bytes). In addition, why it is not necessary to use the "+" operator or some string operator (like strcat) to join "<c>" __DATE__ "</c>"?

Thanks you

> Could you explain why it works using __DATE__ (instead of accessing to its bytes).

Looks like "marginal" compiler specific behavior. Newer GCC and clang allow your original variant (checked on rextester,com)

> why it is not necessary to use the "+" operator or some string operator (like strcat) to join

Because the __DATE__ macro is substituted to a quoted string, and several such strings can be joined without any operators: "string1" "string2" "string3" == "string1string2string3"

Good luck,

-- pa