我遇到了一个奇怪的问题。我使用Eclipse Oxygen.2,Windows 10,J-linker,STM32L4R5ZI,STM32CubeMX V1.0 ver 4.26.0。
问题: 我可以编译程序并在uC上运行它们,但是每当我想使用'malloc'或'new'函数时,都无法编译我的项目并收到以下消息:
../system/src/newlib/_sbrk.c:84: undefined reference to `_Heap_Begin'
../system/src/newlib/_sbrk.c:84: undefined reference to `_Heap_Limit'
有人知道如何解决吗?
主要功能:
int main(void)
{
HAL_Init();
SystemClock_Config();
uint8_t *tab = malloc(100);
MX_GPIO_Init();
MX_TIM1_Init();
HAL_TIM_OC_Start_IT(&htim1, TIM_CHANNEL_1);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
while (1)
{
}
}
_sbrk.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;
}
答案 0 :(得分:2)
_Heap_Begin
和_Heap_Limit
是来自链接器的值。这些应该在您使用的链接描述文件中定义。通常,在CubeMX创建的项目中,它们位于sections.ld
中,如下所示:
PROVIDE ( _Heap_Begin = _end_noinit ) ;
PROVIDE ( _Heap_Limit = __stack - __Main_Stack_Size ) ;
检查这些定义是否存在以及在链接期间是否使用了这些文件-您应该看到它们作为命令行参数传递,例如
arm-none-eabi-g++ -mcpu=cortex-m4 [...] -T "path/to/sections.ld" [...]
这是通过转到项目属性-> C / C ++构建->设置->工具设置选项卡->跨ARM C ++链接器->常规->脚本文件(-T)来控制的。