2016年5月24日 星期二

[STM32] 解決 sprintf...etc 或 maco函數無法編譯的問題

接上一篇,
如果遭遇到, 當使用了一些 standard C 函式庫之後, 且 compile 無法正常編譯過,

Error Message:
c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7-m\libg.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
collect2.exe: error: ld returned 1 exit status
make: *** [USART_String.elf] Error 1


請試著使用以下方法,

1, 在 stm32_flash.ld 檔案找到如下這段, 然後在最後面加上 PROVIDE (__HEAP_START = _end);  

  /* Uninitialized data section */
  . = ALIGN(4);
  .bss :
  {
    /* This is used by the startup in order to initialize the .bss secion */
    _sbss = .;         /* define a global symbol at bss start */
    __bss_start__ = _sbss;
    *(.bss)
    *(.bss*)
    *(COMMON)

    . = ALIGN(4);
    _ebss = .;         /* define a global symbol at bss end */
    __bss_end__ = _ebss;
  } >RAM

  PROVIDE ( end = _ebss );
  PROVIDE ( _end = _ebss );
  PROVIDE (__HEAP_START = _end);

2, 在 USER 資料夾內, 創建一個新的檔案 syscalls.c , 並在裡面添加下面這些 code.

#include <sys/types.h>
// for use malloc sprintf function
extern int  __HEAP_START;

caddr_t _sbrk ( int incr ){
  static unsigned char *heap = NULL;
  unsigned char *prev_heap;

  if (heap == NULL) {
    heap = (unsigned char *)&__HEAP_START;
  }
  prev_heap = heap;


  heap += incr;

  return (caddr_t) prev_heap;
}


儲存, 應該就可以正常編譯了.

沒有留言:

張貼留言