undefined reference to `_sbrk' when using atof() function

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: utzig, lbednarz, tfAteba, barthess, RoccoMarco

Posts: 7
Joined: Fri Mar 21, 2014 5:06 pm

undefined reference to `_sbrk' when using atof() function

Postby maskari » Sun Apr 13, 2014 4:45 pm


I am trying to cast a string as a float in a function, I was successfully able to cast a string as an integer in the same function and I can compile the code without any errors as long as I comment out the atof() part.

my code is:

void cmd_color(BaseSequentialStream *chp, int argc, char *argv[]) {


float fx, fy;
char test[20];
strcpy(test, "123.456");
//test = 123.456;
// fx = atof(argv[0]);
fy = atof(test);
fx = 4.8;
// fy = 2.4;
chprintf(chp, "String values = %s , %s . Float values = %f , %f\r\n", argv[0], argv[1], fx, fy);

// Color(atof(x), atof(y));

//Color(atof(argv[0]), atof(argv[1]));

//printing ratios of the 3 primary LEDs
chprintf(chp, "Ratio of Red LED is %f \r\n", Ratior());
chprintf(chp, "Ratio of Green LED is %f \r\n", Ratiog());
chprintf(chp, "Ratio of Blue LED is %f \r\n", Ratiob());


the above code has been modified from its original context in order to try and debug it, my final aim/goal is to read two variable length strings argv[0] and argv[1] and use them as inputs for the function Color which takes two floats.

the compiler outputs this:

Compiling console.c
console.c:18:30: warning: 'chp' defined but not used [-Wunused-variable]
Linking build/ch.elf
/Users/maskari/Projects/arm/Toolchain/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7e-m/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
collect2: error: ld returned 1 exit status
make: *** [build/ch.elf] Error 1

I am using the make file provided to me by the ChibiOS demo for my board the discovery f4.

my compiler is arm-none-eabi version 4.7
I have the latest version of ChibiOS

any suggestions would be massively appreciated as this error is driving me nuts!

User avatar
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland

Re: undefined reference to `_sbrk' when using atof() functio

Postby Tectu » Sun Apr 13, 2014 5:13 pm

_sbrk is something the compiler does not provide itself. You have to provide the implementation on your own. Here's the necessary documentation: https://sourceware.org/newlib/libc.html#Syscalls
Please read this post for some more detailed information: viewtopic.php?f=2&t=1446

~ Tectu

Posts: 50
Joined: Tue Feb 26, 2013 3:43 pm
Location: The flatlands of East Anglia

Re: undefined reference to `_sbrk' when using atof() functio

Postby theShed » Sun Apr 13, 2014 10:31 pm

A few ways to solve this:

1. implement _sbrk - the reference implementation should work OK. But, a call to _sbrk indicates that dynamic memory allocation is being used and this is generally considered to be a 'bad thing' in an embedded system.

2. roll your own atof - not hard, google 'atof implementation' for ideas.

3. scale the inputs - input integers and divide down by a fixed value to floats. Say, divide by 1000 to give 3 decimal places. eg. input 1234 => divided down gives 1.234

4. re-think the way you are doing things - do you need to use floats? can you do the same thing with integer maths?


Posts: 7
Joined: Fri Mar 21, 2014 5:06 pm

Re: undefined reference to `_sbrk' when using atof() functio

Postby maskari » Wed Apr 16, 2014 3:14 pm

Hi, thanks for the info!
it seems that all I needed to do was add syscalls.c to my makefile and that solved my problem!

Return to “General Support”

Who is online

Users browsing this forum: No registered users and 4 guests