Found the cause of the "unknown type name 'HAL_StatusTypeDef' " error
Today I got bitten by a problem that seems quite common with beginners of the
STM32 C++ / HAL users.
I copied and paste quite a few lines that I found on the internet. And at
one time, the build of the project broke completely.
Errors like :
../Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc_ex.h:6746:1: error: unknown type name ‘HAL_StatusTypeDef’; did you mean ‘FLASH_TypeDef’?
...
18:04:50 Build Failed. 102 errors
I googled the problem (error: unknown type name ‘HAL_StatusTypeDef’), and others had this problem too.
For example :
https://www.openstm32.org/forumthread1225
And many others. But I didn’t find the correct explanation.
The blog of Steve Anderson gave an answer that got me on the way :
https://www.bootladder.com/stm32f7-discovery-create-a-new-app-module.html
The problem is with premature inclusion of
#include “stm32f4xx_hal_def.h”
Some further includes get hidden.
In stm32f4xx_hal_def.h :
#include “stm32f4xx.h”
In stm32f4xx.h (around line 134) :
#elif defined(STM32F407xx)
#include “stm32f407xx.h”
In stm32f407xx.h (around line 167) (large file, more than 15 000 lines :
#include “core_cm4.h” /* Cortex-M4 processor and core peripherals */
#include “system_stm32f4xx.h”
In system_stm32f4xx.h :
#include “stm32f4xx.h”
This is where it gets tricky :
At the start of the file, there is the guard so that the file will not get included twice :
#ifndef __STM32F4xx_H
#define __STM32F4xx_H
Except that the include of “system_stm32f4xx.h” was triggered indirectly by
“stm32f4xx.h”, so _ _STM32F4xx_H is already defined.
So anything that should have been declared below the line 134 of “stm32f4xx.h”
will not be there.
And what do we have below line 134 ? Well, at line 235 :
#if defined (USE_HAL_DRIVER)
#include “stm32f4xx_hal.h”
#endif /* USE_HAL_DRIVER */
“stm32f4xx_hal.h” includes “stm32f4xx_hal_conf.h”
“stm32f4xx_hal_conf.h” includes a lot of files for which the user has uncommented the #define(s).
For example “stm32f4xx_hal_rcc.h”
The first include in those files is :
#include “stm32f4xx_hal_def.h”
But this will not work, as
#define __STM32F4xx_HAL_DEF
has already been executed.
So for me the recommendation would be :
1) Don’t include “stm32f4xx_hal_def.h” directly (unless you really know what you’re doing)
2) Play it safe and just include “stm32f4xx.h”, this will include the rest
Hope this will help not to get trapped in these errors again !