2023-09-30 03:31 AM
Hi,
I have small headless firmware and wish to include a version constant in the Binary image so I can determine the version when using the debugger.
It's easy if your project has a display, but I can't figure out an easy way of doing it for a simple system. If I put a string constant in main.c the linker doesn't include it as the string isn't used anywhere.
Does anyone know of an easy way to do this with an example?
Regards
Rob
Solved! Go to Solution.
2023-09-30 08:29 PM
Hi,
I figured it out:
main.c
static const char version[] __attribute__((used)) __attribute__((section (".version"))) = "Ver: V2.0.0 01/10/2023 13:27";
Linker Script
...
/* Version Data Into Flash */
.version :
{
. = ALIGN(4);
KEEP(*(.version))
KEEP(*(.version*))
. = ALIGN(4);
} >FLASH_CODE
All good
Regards
Rob
2023-09-30 04:12 AM - edited 2023-09-30 04:14 AM
The quickest way is to make a reference to this string somewhere in code or data, so that compiler or linker cannot tell that it is unused.
If you have more time see this thread (about the linker, custom sections, KEEP...)
2023-09-30 08:29 PM
Hi,
I figured it out:
main.c
static const char version[] __attribute__((used)) __attribute__((section (".version"))) = "Ver: V2.0.0 01/10/2023 13:27";
Linker Script
...
/* Version Data Into Flash */
.version :
{
. = ALIGN(4);
KEEP(*(.version))
KEEP(*(.version*))
. = ALIGN(4);
} >FLASH_CODE
All good
Regards
Rob