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. Soo 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.
With kind regards, Ziga.
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: http://www.lpclinux.com/Downloads/WebHome 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 sucess.
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
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.
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.
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.
with visualgdb (using gcc), and nanolib, I had to add the linker flag
-specs=nosys.specs
来源:https://stackoverflow.com/questions/5764414/undefined-reference-to-sbrk