Skip to main content
Lead II
February 20, 2025
Solved

sysmem.c build error in clang

  • February 20, 2025
  • 5 replies
  • 1660 views

When trying to build an STM32CubeMX V6.13.0 generated cmake+gcc project with clang there are two files that generate errors:

  1. sysmem.c
  2. 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:

  1. The easiest is not to use NULL but (void*)0. This is something ST could do. 
  2. including stddef.h . This defines NULL and also ptrdiff_t.
  3. 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:

  1. putting "Memories definition" at the beginning
  2. 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).

Best answer by unsigned_char_array

It works now!


Using STM32CubeIDE for Visual Studio Code

  1. sysmem.c now includes stddef.h
  2. linker script is changed

5 replies

Pavel A.
Super User
February 20, 2025

About NULL: +1.

Even without clang,  It looks like stdint.h from "mainstream" GNU headers does not define NULL. The following causes error with gcc on rextester:

 

//gcc 7.4.0
#include <errno.h>
#include <stdint.h>
int main(void)
{
 return (int)NULL;
}

 

NULL is defined in the newlib headers, not portable.

 

Technical Moderator
February 21, 2025

Hello @unsigned_char_array 

The Cmake project generated by STM32CubeMX is typically designed to work with the GNU Arm Embedded Toolchain.

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question.Saket_Om"
Lead II
February 21, 2025

I know. But there is a bug in sysmem.c that can be fixed with 1 line of code.

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."
Pavel A.
Super User
February 22, 2025

that can be fixed with 1 line of code.
Sorry but things are not that simple in real life. In order to make any tiny change someone has to open a ticket, fill in a bunch of data. Then it should pass code review, possibly some CI too, then someone else should approve, then build deliverables... like that.

 

Semer CHERNI
ST Employee
February 25, 2025

Hello @unsigned_char_array 

First I would like to thank you for posting this request and the level of details you provided.

As my colleague @Saket_Om  mentioned, the STM32CubeMX tool generate code compatible with GNU Arm Embedded Toolchain.

However, the request is under investigation by our teams.

BR,
Semer.

unsigned_char_arrayAuthorBest answer
Lead II
October 2, 2025

It works now!


Using STM32CubeIDE for Visual Studio Code

  1. sysmem.c now includes stddef.h
  2. linker script is changed
"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."
Lead II
November 11, 2025

@Semer CHERNI One question:

The name of the linker file was changed from stm32l476rgtx_flash.ld to STM32L476XX_FLASH.ld. Why was it changed? The reason I ask is that linker files frequently change names. This makes version control harder.

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."