Article directory
1. Problem description
Xiaobai has just learned RT-Thread, and the problems are recorded for easy reference;
Compilation environment: RT-Thread Studio
RT-Thread dynamic creation thread compilation error, a compilation error occurred after compilation, the code prompt: undefined reference to "rt_thread_create";
the code does not have much content, just write a dynamic creation thread in the user's main function , there was also a compilation error, the code is as follows:
int main(void)
{
/************************************************/
rt_thread_t result_thread = NULL;
/*动态创建线程*/
result_thread = rt_thread_create("dynamic_thread1",
dynamic_thread1_entry,
NULL,
1024,
10,
5
);
if(result_thread == RT_NULL){
LOG_E("dynamic_thread1 create fail...\n");
}
/************************************************/
return RT_EOK;
}
2. Problem solving
You can jump to the place where rt_thread_create is defined, but it still prompts that it is not defined. The reason is:
rt_thread_create is a dynamic thread and needs to turn on "memory management". In Memory Management -> Enable Dynamic Memory Settings in RT-Thread Settings, the default is Disable. Heap, you need to select Small Memory Algorithm or SLAB Algorithm for large memory
From the configured source file rtconfig.h, you can see that the macro definition of RT_USING_HEAP is turned on;
after configuration, there will be no prompt that rt_thread_create is not defined;
Others:
Use dynamic memory heap under bare metal conditions: adjust in startup file (startup_stm32f103xe.s):
Heap_Size EQU 0x00000200
and then use malloc() to obtain it
In RT-Thread, if RT_USING_HEAP is defined in rtconfig.h, there will be an initialization code in the rt_hw_board_init() function in the source file board.c
/* Heap initialization */
#if defined(RT_USING_HEAP)
rt_system_heap_init((void *) HEAP_BEGIN, (void *) HEAP_END);
#endif