sysmem.c build error in clang
When trying to build an STM32CubeMX V6.13.0 generated cmake+gcc project with clang there are two files that generate errors:
- sysmem.c
- the linker script (STM32L431RCTX_FLASH.ld and stm32l476rgtx_flash.ld)
sysmem.c produces the following error:
Core/Src/sysmem.c:30:35: error: use of undeclared identifier 'NULL'
[build] 30 | static uint8_t *__sbrk_heap_end = NULL;
This is caused by the fact that only errno.h and stdint.h are included. None of them officially define NULL. But they probably do in gcc. Also ptrdiff_t is undefined.
sysmem.c can easily be modified by either:
- The easiest is not to use NULL but (void*)0. This is something ST could do.
- including stddef.h . This defines NULL and also ptrdiff_t.
- or modifying build to forcebly include such a file in all files (adding "-include stddef.h" to compile command). I did this so I wouldn't have to modify the file (which would get overwritten by regenerating code). But it's kind of a hack.
Linker script:
Linker scmemory region not defined: RAM
[build] >>> _estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */ript generates the following errors:
and
[build] ld.lld: error: STM32L431RCTX_FLASH_clang.ld:93: symbol not found: READONLY
[build] ld.lld: error: unable to place section .ARM.extab at file offset [0xFFFFFFFFF8010000, 0xFFFFFFFFF800FFFF]; check your linker script for overflows
Linker script can easily be modified by:
- putting "Memories definition" at the beginning
- and removing the "(READONLY)" parts and replace them with a space (there has to be a space before the colon)
Modifying sysmem.c would have priority in my opinion since linker scripts often need to be modified anyway.
The resulting code now builds with both clang and gcc without errors (many warnings though, but that is kind of the point for me, so I can run clang-tidy and parse the syntax tree).
