STM32 HAL库 error: #20: identifier "HAL_StatusTypeDef" is undefined问题

本文分享了一次使用STM32L0芯片及HAL库时遇到的编译错误经历,详细介绍了如何从包含头文件的细节出发解决问题的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

error: #20: identifier “HAL_StatusTypeDef” is undefined

我使用的是stm32l0的芯片,由于没有标准库可用,无奈只有折腾HAL库了。

首先,说下上面的错误怎么来的。我使用stm32cubeMX自动生成了代码,并且自己在工程里面新建了文件,文件里面需要用到HAL外设库中定义的宏定义,本来应该添加包含“stm32l0xx_hal_dac.h”,但是我发现”stm32l0xx_hal_conf.h” 中有所有外设的头文件,并且通过宏定义打开,所以我在该文件中添加了下面的语句:

#include "stm32l0xx_hal_conf.h" 

编译后就出现了如下错误:
../Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal.h(374): error: #20: identifier
“HAL_StatusTypeDef” is undefined
HAL_StatusTypeDef HAL_Init(void);
../Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal.h(375): error: #20: identifier
“HAL_StatusTypeDef” is undefined
HAL_StatusTypeDef HAL_DeInit(void);
../Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal.h(378): error: #20: identifier
“HAL_StatusTypeDef” is undefined
HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority);
..\Drv\dac\drv_dac.c: 0 warnings, 3 errors

一直想不明白,“HAL_StatusTypeDef”变量在“stm32l0xx_hal_def.h”中已经有了定义,并且该变量也能成功跳转被找到。我已经包含了宏定义文件“stm32l0xx_hal_conf.h”,宏定义”HAL_DAC_MODULE_ENABLED“也使能了,添加的文件中变量也能成功跳转找到“stm32l0xx_hal_dac.h”中的定义,为什么会报错!

后来,我在整理main.c文件发现,其添加的头文件是“stm32l0xx_hal.h”,而不是文件“stm32l0xx_hal_conf.h”,于是,我将自己添加文件中改为

#include "stm32l0xx_hal.h" 

再次编译工程,居然编译成功了。其中的原因,我还没有想明白,想明白了再回来补上。如果有大神知道原因,请指教!

Build target &#39;Target 1&#39; compiling main.c... ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(296): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_Init(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(297): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_DeInit(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(300): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(300): error: #20: identifier "uint32_t" is undefined HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(310): error: #20: identifier "uint32_t" is undefined void HAL_Delay(uint32_t Delay); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(311): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetTick(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(312): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetTickPrio(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(313): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(317): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetHalVersion(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(318): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetREVID(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(319): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetDEVID(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(326): error: #20: identifier "uint32_t" is undefined void HAL_GetUID(uint32_t *UID); dht11.h(12): error: #20: identifier "GPIO_TypeDef" is undefined void DHT11_Init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); dht11.h(12): error: #20: identifier "uint16_t" is undefined void DHT11_Init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); dht11.h(13): error: #20: identifier "uint8_t" is undefined DHT11_Status DHT11_Read(uint8_t *temperature, uint8_t *humidity); dht11.h(13): error: #20: identifier "uint8_t" is undefined DHT11_Status DHT11_Read(uint8_t *temperature, uint8_t *humidity); main.c(9): error: #20: identifier "SPI_HandleTypeDef" is undefined extern SPI_HandleTypeDef hspi1; main.c(10): error: #20: identifier "TIM_HandleTypeDef" is undefined extern TIM_HandleTypeDef htim2; main.c(15): warning: #223-D: function "SystemClock_Config" declared implicitly SystemClock_Config(); main.c(16): warning: #223-D: function "MX_GPIO_Init" declared implicitly MX_GPIO_Init(); main.c(17): warning: #223-D: function "MX_SPI1_Init" declared implicitly MX_SPI1_Init(); // ??SPI??? main.c(18): warning: #223-D: function "MX_TIM2_Init" declared implicitly MX_TIM2_Init(); // ??TIM2??? main.c(24): error: #159: declaration is incompatible with previous "MX_TIM2_Init" (declared at line 18) static void MX_TIM2_Init(void) { main.c(25): error: #20: identifier "TIM_ClockConfigTypeDef" is undefined TIM_ClockConfigTypeDef sClockSourceConfig = {0}; main.c(26): error: #20: identifier "TIM_MasterConfigTypeDef" is undefined TIM_MasterConfigTypeDef sMasterConfig = {0}; main.c(28): error: #20: identifier "TIM2" is undefined htim2.Instance = TIM2; main.c(30): error: #20: identifier "TIM_COUNTERMODE_UP" is undefined htim2.Init.CounterMode = TIM_COUNTERMODE_UP; main.c(32): error: #20: identifier "TIM_CLOCKDIVISION_DIV1" is undefined htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; main.c(33): error: #20: identifier "TIM_AUTORELOAD_PRELOAD_DISABLE" is undefined htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; main.c(34): warning: #223-D: function "HAL_TIM_Base_Init" declared implicitly if (HAL_TIM_Base_Init(&htim2) != HAL_OK) { main.c(34): error: #20: identifier "HAL_OK" is undefined if (HAL_TIM_Base_Init(&htim2) != HAL_OK) { main.c(35): warning: #223-D: function "Error_Handler" declared implicitly Error_Handler(); main.c(37): error: #20: identifier "TIM_CLOCKSOURCE_INTERNAL" is undefined sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; main.c(38): warning: #223-D: function "HAL_TIM_ConfigClockSource" declared implicitly if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) { main.c(38): error: #20: identifier "HAL_OK" is undefined if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) { main.c(39): warning: #223-D: function "Error_Handler" declared implicitly Error_Handler(); main.c(41): error: #20: identifier "TIM_TRGO_RESET" is undefined sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; main.c(42): error: #20: identifier "TIM_MASTERSLAVEMODE_DISABLE" is undefined sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; main.c: 8 warnings, 30 errors compiling dht11.c... ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(296): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_Init(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(297): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_DeInit(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(300): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(300): error: #20: identifier "uint32_t" is undefined HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(310): error: #20: identifier "uint32_t" is undefined void HAL_Delay(uint32_t Delay); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(311): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetTick(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(312): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetTickPrio(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(313): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(317): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetHalVersion(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(318): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetREVID(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(319): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetDEVID(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(326): error: #20: identifier "uint32_t" is undefined void HAL_GetUID(uint32_t *UID); dht11.h(12): error: #20: identifier "GPIO_TypeDef" is undefined void DHT11_Init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); dht11.h(12): error: #20: identifier "uint16_t" is undefined void DHT11_Init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); dht11.h(13): error: #20: identifier "uint8_t" is undefined DHT11_Status DHT11_Read(uint8_t *temperature, uint8_t *humidity); dht11.h(13): error: #20: identifier "uint8_t" is undefined DHT11_Status DHT11_Read(uint8_t *temperature, uint8_t *humidity); dht11.c(7): warning: #223-D: function "__HAL_TIM_SET_COUNTER" declared implicitly __HAL_TIM_SET_COUNTER(&htim2, 0); dht11.c(7): error: #20: identifier "htim2" is undefined __HAL_TIM_SET_COUNTER(&htim2, 0); dht11.c(8): warning: #223-D: function "HAL_TIM_Base_Start" declared implicitly HAL_TIM_Base_Start(&htim2); dht11.c(9): warning: #223-D: function "__HAL_TIM_GET_COUNTER" declared implicitly while (__HAL_TIM_GET_COUNTER(&htim2) < us); dht11.c(10): warning: #223-D: function "HAL_TIM_Base_Stop" declared implicitly HAL_TIM_Base_Stop(&htim2); dht11.c(6): warning: #177-D: function "delay_us" was declared but never referenced static void delay_us(uint16_t us) { dht11.c: 5 warnings, 17 errors compiling buzzer.c... ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\CMSIS\Device\ST\STM32F1xx\Include\stm32f1xx.h(159): error: #35: #error directive: "Please select first the target STM32F1xx device used in your application (in stm32f1xx.h file)" #error "Please select first the target STM32F1xx device used in your application (in stm32f1xx.h file)" buzzer.c: 0 warnings, 1 error compiling TFT_GFX.c... ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(296): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_Init(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(297): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_DeInit(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(300): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(300): error: #20: identifier "uint32_t" is undefined HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(310): error: #20: identifier "uint32_t" is undefined void HAL_Delay(uint32_t Delay); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(311): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetTick(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(312): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetTickPrio(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(313): error: #20: identifier "HAL_StatusTypeDef" is undefined HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(317): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetHalVersion(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(318): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetREVID(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(319): error: #20: identifier "uint32_t" is undefined uint32_t HAL_GetDEVID(void); ..\STM32CubeF1\STM32Cube_FW_F1_V1.6.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h(326): error: #20: identifier "uint32_t" is undefined void HAL_GetUID(uint32_t *UID); TFT_GFX.c(2): error: #5: cannot open source input file "spi.h": No such file or directory #include "spi.h" TFT_GFX.c: 0 warnings, 13 errors compiling TFT_FONTS.c... TFT_FONTS.c(5): error: #20: identifier "Font8x16_Data" is undefined FontDef Font_8x16 = {8, 16, Font8x16_Data}; TFT_FONTS.c: 0 warnings, 1 error ".\Objects\zuoye.axf" - 62 Error(s), 13 Warning(s). Target not created. Build Time Elapsed: 00:00:01
最新发布
06-26
…/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_rcc_ex.h(1271): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_rcc.h(3357): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_RCC_DeInit(void); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_rcc.h(3358): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_rcc.h(3359): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_gpio.h(301): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_dma.h(119): error: #20: identifierHAL_LockTypeDef” is undefined HAL_LockTypeDef Lock; /!< DMA locking object */ …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_dma_ex.h(197): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_DMAEx_ConfigMuxRequestGenerator(DMA_HandleTypeDef *hdma, …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_dma_ex.h(199): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_DMAEx_EnableMuxRequestGenerator(DMA_HandleTypeDef *hdma); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_dma_ex.h(200): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_DMAEx_DisableMuxRequestGenerator(DMA_HandleTypeDef *hdma); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_dma_ex.h(204): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_DMAEx_ConfigMuxSync(DMA_HandleTypeDef *hdma, HAL_DMA_MuxSyncConfigTypeDef *pSyncConfig); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_dma.h(759): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_dma.h(760): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_dma.h(769): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength); …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_dma.h(770): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, …/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_dma.h(772): error: #20: identifierHAL_StatusTypeDefis undefined HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma);
03-29
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值