CubeMXImporter icon indicating copy to clipboard operation
CubeMXImporter copied to clipboard

Error: "multiple definition of `_sbrk'" in src/syscalls.c

Open tqthangdq opened this issue 4 years ago • 4 comments

I am using STM32F4 Discovery board following the step by step in 'Mastering STM32' - Carmine Noviello (Aug-2018). I have tried to import the CubeMX code generated project to the existed project in eclipse and it has got this error. Eclipse: Oxygen.3a Release (4.7.3a) Cube MX: 5.6.1 ARM GCC: gcc-arm-none-eabi-9-2019-q4-major-win32

c:/smt32toolchain/gcc-arm/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: ./src/syscalls.o: in function _sbrk': C:\SMT32Toolchain\projects\Blinky\Debug/../src/syscalls.c:118: multiple definition of _sbrk'; ./system/src/newlib/_sbrk.o:C:\SMT32Toolchain\projects\Blinky\Debug/../system/src/newlib/_sbrk.c:45: first defined here collect2.exe: error: ld returned 1 exit status make: *** [makefile:64: Blinky.elf] Error 1

Thank you!

tqthangdq avatar May 29 '20 17:05 tqthangdq

I have checked on the Internet and find out this blog works fine with my issue: https://mcuoneclipse.com/2014/03/16/freertos-malloc-and-sp-check-with-gnu-tools/

However, I don't understand why this function "_sbrk" has been declared multiple times? Thanks.

tqthangdq avatar May 29 '20 17:05 tqthangdq

Had this same issue. I fixed it by deleting newlib/_sbrk.c.

haydenroche5 avatar Jul 19 '20 14:07 haydenroche5

I've been having the same issue. For a temporary fix, on the src/syscalls.c file, comment out the _sbrk() function. So from line 117 to 138, comment it out. This should compile the program without any error.

Would love to know if there are any permanent fix for this though.

Lines in question:

caddr_t _sbrk(int incr)
{
	extern char end asm("end");
	static char *heap_end;
	char *prev_heap_end;

	if (heap_end == 0)
		heap_end = &end;

	prev_heap_end = heap_end;
	if (heap_end + incr > stack_ptr)
	{
//		write(1, "Heap and stack collision\n", 25);
//		abort();
		errno = ENOMEM;
		return (caddr_t) -1;
	}

	heap_end += incr;

	return (caddr_t) prev_heap_end;
}

tiazahmd avatar Aug 09 '20 01:08 tiazahmd

Have the same issue. Compiled successfully after omitting system/src/newlib/_sbrk.c from build.

The newlib implementation is different than the one in src/syscalls.c:

_sbrk(int incr)
{
  extern char _Heap_Begin; // Defined by the linker.
  extern char _Heap_Limit; // Defined by the linker.

  static char* current_heap_end;
  char* current_block_address;

  if (current_heap_end == 0)
    {
      current_heap_end = &_Heap_Begin;
    }

  current_block_address = current_heap_end;

  // Need to align heap to word boundary, else will get
  // hard faults on Cortex-M0. So we assume that heap starts on
  // word boundary, hence make sure we always add a multiple of
  // 4 to it.
  incr = (incr + 3) & (~3); // align value to 4
  if (current_heap_end + incr > &_Heap_Limit)
    {
      // Some of the libstdc++-v3 tests rely upon detecting
      // out of memory errors, so do not abort here.
#if 0
      extern void abort (void);

      _write (1, "_sbrk: Heap and stack collision\n", 32);

      abort ();
#else
      // Heap has overflowed
      errno = ENOMEM;
      return (caddr_t) - 1;
#endif
    }

  current_heap_end += incr;

  return (caddr_t) current_block_address;
}

I don't have enough C/Arm knowledge. Which one is outdated?

git-dc avatar Feb 10 '21 17:02 git-dc