Arduino Due and C++

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So I developed a nRF24L01+ wireless library and it's working great so I want to convert it to C++ along with some other stuff I developed.

 

Entered a simple C++ program builds and runs fine so I start throwing a bunch of 0s and 1s at the editor and get serious.

 

Everything works fine until I use new or malloc then I start getting;

 

undefined reference to _sbrk

 

I've googled for hours and have found and tried several things none of which worked, something about newlib keeps coming up but I'm not getting it.

 

Any clue?

 

HELP SEND CODZ PLZ  :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to define this function. Newlib libc library is platform independent and does not know anything about memory allocation on specific platforms.

 

Here is a simplest example of such function:

int8_t *_sbrk(int incr)
{
  static uint8_t heap[100];
  static uint8_t *heap_end;
  uint8_t *prev_heap_end;

  if (0 == heap_end)
    heap_end = heap;

  prev_heap_end = heap_end;

  if ((heap_end + incr) >= (heap + sizeof(heap)))
    return NULL;

  heap_end += incr;
  return prev_heap_end;
}

This will allow allocation of up to 100 bytes. Change the array size to allow more. You can also set boundaries for the heap in the linker script and change the function to return memory within those boundaries.

NOTE: I no longer actively read this forum. Please ask your question on www.eevblog.com/forum if you want my answer.

Last Edited: Thu. Oct 23, 2014 - 07:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Alex,

Thanks for your help.

I noticed the linker scripts but really had no idea what they were used for.  So  I guess the line;  HEAP_SIZE = DEFINED(HEAP_SIZE) ? HEAP_SIZE : 0x200;  would be were I set the size of the heap

and the line;    ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00018000 would be the upper and lower bounds to check for?

 

So I get from your response and sample code we need to do our own heap management? Really?

 

I tried you code and some a more comprehensive example I got from another site and in both cases I ended up with;

 

This is the instantiation in question.

CSpi spi = new CSpi(PIOC, PIO_PC12, PIO_PC13, PIO_PC14);

and this is the resultant ouput

Warning	1	unused variable 'spi_test' [-Wunused-variable]	J:\Robotics Projects\ARM Projects\IDuino Due C++ Library Projects\Due_Library_Harness\Due_Library_Harness\Due_Library_Harness.cpp	48	8	Due_Library_Harness
Error	6	undefined reference to `_write'	/home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/reent/writer.c	58	1	Due_Library_Harness
Error	3	undefined reference to `_sbrk'	/home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/reent/sbrkr.c	58	1	Due_Library_Harness
Error	11	undefined reference to `_read'	/home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/reent/readr.c	58	1	Due_Library_Harness
Error	10	undefined reference to `_lseek'	/home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/reent/lseekr.c	58	1	Due_Library_Harness
Error	4	undefined reference to `_kill'	/home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/reent/signalr.c	61	1	Due_Library_Harness
Error	9	undefined reference to `_isatty'	/home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/reent/isattyr.c	58	1	Due_Library_Harness
Error	5	undefined reference to `_getpid'	/home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/reent/signalr.c	96	1	Due_Library_Harness
Error	8	undefined reference to `_fstat'	/home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/reent/fstatr.c	62	1	Due_Library_Harness
Error	2	undefined reference to `_exit'	/home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/stdlib/abort.c	63	1	Due_Library_Harness
Error	7	undefined reference to `_close'	/home/tools/hudson/workspace/arm-gnu-toolchain/.build/src/newlib-custom/newlib/libc/reent/closer.c	53	1	Due_Library_Harness
Error	12	ld returned 1 exit status	collect2.exe	0	0	Due_Library_Harness
Last Edited: Thu. Oct 23, 2014 - 11:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We've discussed all this before here - principally when trying to get printf() running on a Cortex because vfprintf() uses malloc(). Do a search for "__sbrk" and you should hit the threads.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks clawson got the answer I needed to solve the problem and it works but what a PITA to have to go through.

I remember going through a similar gyration when AS first came out trying to get C++ working with 8bit.