Keil编译报错[error: #101: “xxx_IRQn“ has already been declared in the current scope]解决办法

库类型:HAL库

突然出现大量报错

提示错误出现的文件是stm32f10x.h;

二、解决办法

出现这种问题是由于代码中存在两个功能重复的头文件
搜索stm32f10x.h的位置;
把stm32f10x.h 替换成stm32f10x_hal.h就可以了,或者去掉

问题:

第一张图:很多报错,

第二张图:跳转到第一个报错的位置

去掉:

将第一个空定义去掉,main.c里面已经有了

之后就好了

Build started: Project: Demo00 *** Using Compiler &#39;V5.06 update 6 (build 750)&#39;, folder: &#39;D:\My Projects\Work Softwares\Keil MDK\ARM\ARMCC\Bin&#39; Build target &#39;Demo00&#39; compiling OLED.c... ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(50): warning: #47-D: incompatible redefinition of macro "__MPU_PRESENT" (declared at line 163 of "D:\My Projects\Work Softwares\Keil MDK\Keil\STM32F1xx_DFP\2.3.0\Device\Include\stm32f10x.h") #define __MPU_PRESENT 0U /*!< Other STM32 devices does not provide an MPU */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(51): warning: #47-D: incompatible redefinition of macro "__NVIC_PRIO_BITS" (declared at line 165 of "D:\My Projects\Work Softwares\Keil MDK\Keil\STM32F1xx_DFP\2.3.0\Device\Include\stm32f10x.h") #define __NVIC_PRIO_BITS 4U /*!< STM32 uses 4 Bits for the Priority Levels */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(52): warning: #47-D: incompatible redefinition of macro "__Vendor_SysTickConfig" (declared at line 166 of "D:\My Projects\Work Softwares\Keil MDK\Keil\STM32F1xx_DFP\2.3.0\Device\Include\stm32f10x.h") #define __Vendor_SysTickConfig 0U /*!< Set to 1 if different SysTick Config is used */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(71): error: #101: "NonMaskableInt_IRQn" has already been declared in the current scope NonMaskableInt_IRQn = -14, /*!< 2 Non Maskable Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(73): error: #101: "MemoryManagement_IRQn" has already been declared in the current scope MemoryManagement_IRQn = -12, /*!< 4 Cortex-M3 Memory Management Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(74): error: #101: "BusFault_IRQn" has already been declared in the current scope BusFault_IRQn = -11, /*!< 5 Cortex-M3 Bus Fault Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(75): error: #101: "UsageFault_IRQn" has already been declared in the current scope UsageFault_IRQn = -10, /*!< 6 Cortex-M3 Usage Fault Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(76): error: #101: "SVCall_IRQn" has already been declared in the current scope SVCall_IRQn = -5, /*!< 11 Cortex-M3 SV Call Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(77): error: #101: "DebugMonitor_IRQn" has already been declared in the current scope DebugMonitor_IRQn = -4, /*!< 12 Cortex-M3 Debug Monitor Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(78): error: #101: "PendSV_IRQn" has already been declared in the current scope PendSV_IRQn = -2, /*!< 14 Cortex-M3 Pend SV Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(79): error: #101: "SysTick_IRQn" has already been declared in the current scope SysTick_IRQn = -1, /*!< 15 Cortex-M3 System Tick Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(82): error: #101: "WWDG_IRQn" has already been declared in the current scope WWDG_IRQn = 0, /*!< Window WatchDog Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(83): error: #101: "PVD_IRQn" has already been declared in the current scope PVD_IRQn = 1, /*!< PVD through EXTI Line detection Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(84): error: #101: "TAMPER_IRQn" has already been declared in the current scope TAMPER_IRQn = 2, /*!< Tamper Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(85): error: #101: "RTC_IRQn" has already been declared in the current scope RTC_IRQn = 3, /*!< RTC global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(86): error: #101: "FLASH_IRQn" has already been declared in the current scope FLASH_IRQn = 4, /*!< FLASH global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(87): error: #101: "RCC_IRQn" has already been declared in the current scope RCC_IRQn = 5, /*!< RCC global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(88): error: #101: "EXTI0_IRQn" has already been declared in the current scope EXTI0_IRQn = 6, /*!< EXTI Line0 Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(89): error: #101: "EXTI1_IRQn" has already been declared in the current scope EXTI1_IRQn = 7, /*!< EXTI Line1 Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(90): error: #101: "EXTI2_IRQn" has already been declared in the current scope EXTI2_IRQn = 8, /*!< EXTI Line2 Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(91): error: #101: "EXTI3_IRQn" has already been declared in the current scope EXTI3_IRQn = 9, /*!< EXTI Line3 Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(92): error: #101: "EXTI4_IRQn" has already been declared in the current scope EXTI4_IRQn = 10, /*!< EXTI Line4 Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(93): error: #101: "DMA1_Channel1_IRQn" has already been declared in the current scope DMA1_Channel1_IRQn = 11, /*!< DMA1 Channel 1 global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(94): error: #101: "DMA1_Channel2_IRQn" has already been declared in the current scope DMA1_Channel2_IRQn = 12, /*!< DMA1 Channel 2 global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(95): error: #101: "DMA1_Channel3_IRQn" has already been declared in the current scope DMA1_Channel3_IRQn = 13, /*!< DMA1 Channel 3 global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(96): error: #101: "DMA1_Channel4_IRQn" has already been declared in the current scope DMA1_Channel4_IRQn = 14, /*!< DMA1 Channel 4 global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(97): error: #101: "DMA1_Channel5_IRQn" has already been declared in the current scope DMA1_Channel5_IRQn = 15, /*!< DMA1 Channel 5 global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(98): error: #101: "DMA1_Channel6_IRQn" has already been declared in the current scope DMA1_Channel6_IRQn = 16, /*!< DMA1 Channel 6 global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(99): error: #101: "DMA1_Channel7_IRQn" has already been declared in the current scope DMA1_Channel7_IRQn = 17, /*!< DMA1 Channel 7 global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(100): error: #101: "ADC1_2_IRQn" has already been declared in the current scope ADC1_2_IRQn = 18, /*!< ADC1 and ADC2 global Interrupt */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(101): error: #101: "USB_HP_CAN1_TX_IRQn" has already been declared in the current scope USB_HP_CAN1_TX_IRQn = 19, /*!< USB Device High Priority or CAN1 TX Interrupts */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(102): error: #101: "USB_LP_CAN1_RX0_IRQn" has already been declared in the current scope USB_LP_CAN1_RX0_IRQn = 20, /*!< USB Device Low Priority or CAN1 RX0 Interrupts */ ../Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h(103): error: #101: "CAN1_RX1_IRQn" has already been declared in the current scope CAN1_RX1_IRQn = 21, /*!< CAN1 RX1 Interrupt */ ..\Core\Src\OLED.c: 3 warnings, 30 errors "Demo00\Demo00.axf" - 30 Error(s), 3 Warning(s). Target not created. Build Time Elapsed: 00:00:01
最新发布
11-02
你遇到的错误: ``` error: #101: "NonMaskableInt_IRQn" has already been declared in the current scope warning: #47-D: incompatible redefinition of macro "__MPU_PRESENT" ... "Demo00\Demo00.axf" - 30 Error(s), 3 Warning(s) ``` 是 **Keil 工程中头文件冲突** 的典型问题,尤其是当你同时包含了 **旧版标准外设库(STM32F1xx StdPeriph)和新版 HAL 库(CMSIS + STM32Cube)** 时。 --- ## 🔍 根本原因分析 你的工程中同时引入了两个定义中断向量和核心寄存器的头文件: 1. `stm32f10x.h` —— 来自 **旧的标准外设库(Standard Peripheral Library)** 2. `stm32f103xe.h` —— 来自 **新的 CMSIS 头文件(由 STM32Cube 提供)** 这两个文件都定义了相同的宏(如 `__NVIC_PRIO_BITS`)和枚举类型(如 `NonMaskableInt_IRQn`),导致 **重复定义错误**。 > ✅ 正确做法:只能使用其中一套头文件系统,不能混用! --- ## ✅ 解决方案(四步走) ### ✅ 第一步:移除旧的标准外设库头文件包含 检查以下位置是否包含了 `stm32f10x.h` 或相关旧库头文件: - `main.h` - `OLED.h` - `stm32f1xx_hal_conf.h` - 或任何 `.c/.h` 文件中的 `#include "stm32f10x.h"` 🔍 找到类似这行代码并删除它: ```c #include "stm32f10x.h" // ❌ 删除这一行! ``` ✔️ 替换为: ```c #include "stm32f1xx_hal.h" // ✅ 使用 HAL 库主头文件即可 ``` > ⚠️ `stm32f1xx_hal.h` 内部会自动包含 `stm32f103xe.h`(根据芯片型号),不需要你自己再包含。 --- ### ✅ 第二步:确认预定义宏设置正确 在 Keil 中配置正确的编译宏,告诉 HAL 库你使用的是哪种芯片。 🔧 操作路径: > Project → Options for Target → C/C++ → Define ✅ 必须包含以下宏(以 STM32F103VE 为例): ``` USE_HAL_DRIVER,STM32F103xE ``` 📌 常见错误: - 缺少 `USE_HAL_DRIVER` → HAL 初始化函数不工作 - 缺少 `STM32F103xE` → 芯片识别错误,可能导致包含错误的头文件 ❌ 错误示例: ``` STM32F10X_MD, USE_STDPERIPH_DRIVER // 这是旧库的宏,必须删除! ``` > 如果存在这些旧宏,请立即删除! --- ### ✅ 第三步:清理并重建工程 由于之前的错误可能已经生成了缓存对象文件,需要彻底清理: 1. 点击菜单:**Project → Clean Files** 2. 再点击:**Project → Rebuild all target files** 这样确保所有 `.o` 文件重新编译,避免残留旧定义。 --- ### ✅ 第四步:验证目录结构与包含路径 确保你没有手动将旧版 StdPeriph 库的 `inc/` 目录加入 Include Paths。 🔧 检查: > Project → Options → C/C++ → Include Paths ❌ 移除以下路径(如果存在): ``` D:\My Projects\Work Softwares\Keil MDK\Keil\STM32F1xx_DFP\2.3.0\Device\Include ``` ✅ 只保留如下合理路径: ``` .\Core\Inc .\Drivers\CMSIS\Device\ST\STM32F1xx\Include .\Drivers\CMSIS\Include .\Drivers\STM32F1xx_HAL_Driver\Inc .\User\OLED ``` > ✔️ 新版 HAL 库所需的头文件都在 `Drivers/` 子目录下,无需引用 Keil 安装目录中的老版本! --- ## 🧩 补充说明:为什么会出现这个冲突? | 项目 | 旧标准外设库(StdPeriph) | 新 HAL 库(Cube) | |------|--------------------------|------------------| | 头文件 | `stm32f10x.h` | `stm32f1xx.h` / `stm32f103xe.h` | | 中断定义 | 在 `stm32f10x.h` 中 | 在 `stm32f103xe.h` 中 | | 宏定义 | `STM32F10X_MD`, `USE_STDPERIPH_DRIVER` | `STM32F103xE`, `USE_HAL_DRIVER` | | 是否推荐 | ❌ 已废弃 | ✅ 推荐使用 | 👉 你现在的问题就是:**HAL 库已经包含了新的 CMSIS 头文件,但你还手动或间接包含了旧的 `stm32f10x.h`,造成重复定义。** --- ## ✅ 最终修复总结 | 步骤 | 操作 | |------|------| | 1 | 删除所有 `#include "stm32f10x.h"` | | 2 | 确保只包含 `#include "stm32f1xx_hal.h"` | | 3 | 删除预定义宏中的 `STM32F10X_xx` 和 `USE_STDPERIPH_DRIVER` | | 4 | 添加预定义宏:`USE_HAL_DRIVER,STM32F103xE` | | 5 | 清理 Include Paths,移除指向旧 DFP 的路径 | | 6 | Clean + Rebuild 工程 | --- ### ✅ 示例:正确的 `main.h` 结构 ```c #ifndef __MAIN_H__ #define __MAIN_H__ #ifdef __cplusplus extern "C" { #endif #include "stm32f1xx_hal.h" // ✅ 唯一需要的头文件 void Error_Handler(void); #ifdef __cplusplus } #endif #endif /* __MAIN_H__ */ ``` `OLED.h` 中也只需: ```c #include "stm32f1xx_hal.h" ``` 不要加 `stm32f10x.h`! --- 完成以上修改后,重新编译,你会发现: - 警告 `#47-D` 消失 - 错误 `#101` 全部消失 - `OLED.c` 成功编译 - 后续链接也能找到 `oled.o` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值