Undefined reference to _sbrk
Asked Answered
D

6

24

I am having a problem with _sbrk. In a link phase of compilation i use below comand to link my objects and i get undefined reference to _sbrk.

arm-none-eabi-ld -static -T linkerscript.ld -o exe timer_example.o /home/ziga/projects/cs_lite/arm-none-eabi/lib/libc.a /home/ziga/projects/cs_lite/lib/gcc/arm-none-eabi/4.5.1/libgcc.a

I am compiling for arm926ej-s and in ARM mode so i think i have chosen the right multilib (libc.a and libgcc.a) which is located in folder home/ziga/projects/cs_lite/arm-none-eabi/lib/.

I have been searching internet for _sbrk function and it is some sort of a memory managment call which isnt included in standard C libraries as it is dependant on microprocessor. So do I have to write _sbrk function on my own? How do I do it? Do you have any example for arm926ej-s? After writing this function I intend to compile it into an object file and link it together with other objects, libraries.

Dressler answered 23/4, 2011 at 13:25 Comment(0)
D
1

I solved this problem and will post solution here so i give something back to comunity. The function _sbrk is located inside NXP CDL package for ARM. Package is available for download (link is for all who dont know this already) here in subfolder CDL_v005/csps/lpc313x/bsps/ea3131/source you will find source file named libnosys_gnu.c which should be added to the project and compiled to object file and after that linked to executable file alongside other objects and libraries.

Best wishes and a lot of success.

Dressler answered 22/4, 2021 at 16:41 Comment(0)
G
23

This helps:

-mcpu=cortex-m4 -mthumb -specs=nano.specs -specs=nosys.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard

The important switches "seem" to be:

-specs=nano.specs -specs=nosys.specs

Gimcrackery answered 9/1, 2017 at 19:46 Comment(0)
J
11

I was having the same problem, and adding those to the linker flags helped:

-specs=nano.specs -specs=nosys.specs

Also, just with the nosys.specs fixed the issue, but the code size was a lot bigger.

Jethro answered 14/11, 2018 at 13:34 Comment(0)
S
4

The problem has little to do with _sbrk itself, but rather your attempt to invoke the linker directly, bypassing the compiler driver. Instead, use the gcc command to invoke the linker and the -Wl,-linkeroptionhere syntax to pass extra options to the linker.

One possible solution, if you must invoke the linker yourself.. Try repeating both libc.a and libgcc.a a second time at the end of the command line. There's also some "as group" linker option you could use to achieve this but I don't know it right off.

Scarificator answered 23/4, 2011 at 15:53 Comment(0)
H
4

recently I also ran into this(again). the easiest solution which worked for me was to provide/redirect "malloc" and "free" apis to the one available from the SDK on which I was building my application.

Basically it happens because of mem management apis missing while linking. like the above answer mentions its not that _sbrk is specifically missing here. brk/sbrk syscall intenrally is used for heap management. hence the _sbrk ,missing link when it comes to mem management apis.

I noticed that adding -lnosys (i.e libnosys.a) also helped a this to a degree in some integrations.

Houseyhousey answered 11/3, 2016 at 7:56 Comment(0)
V
4

with visualgdb (using gcc), and nanolib, I had to add the linker flag

-specs=nosys.specs
Virgate answered 24/2, 2019 at 10:11 Comment(0)
D
1

I solved this problem and will post solution here so i give something back to comunity. The function _sbrk is located inside NXP CDL package for ARM. Package is available for download (link is for all who dont know this already) here in subfolder CDL_v005/csps/lpc313x/bsps/ea3131/source you will find source file named libnosys_gnu.c which should be added to the project and compiled to object file and after that linked to executable file alongside other objects and libraries.

Best wishes and a lot of success.

Dressler answered 22/4, 2021 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.