embeddedsbrkchibios

Undefined reference to _sbrk in ChibiOS


I'm using ChibiOS 3.x to write an embedded application. When compiling/linking it, I encounter an error message like this:

/usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7e-m/libg.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'

Where is _sbrk defined and how can I resolve this issue?


Solution

  • _sbrk is defined in os/various/syscalls.c, so ensure this file is properly compiled and linked.

    Note that _sbrk will work correctly if and only if CH_CFG_USE_MEMCORE == TRUE (you can change that in chconf.h). Else, malloc() will always return 0 and result in an errno of ENOMEM. Also see this answer for a more thorough explanation of the ENOMEM issue.

    Also note that for embedded applications, especially when targetting high reliability, it might not be advisable to use dynamic memory allocation (of which _sbrk is a part of) at all (see this detailed explanation)