Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tqthangdq opened this issue May 29, 2020 · 4 comments
Open

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

tqthangdq opened this issue May 29, 2020 · 4 comments

Comments

@tqthangdq
Copy link

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
Copy link
Author

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.

@haydenroche5
Copy link

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

@tiazahmd
Copy link

tiazahmd commented Aug 9, 2020

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;
}

@git-dc
Copy link

git-dc commented Feb 10, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants