解决 error:identifier “xxx” is undefined

博客主要讲述了报错问题及解决方法。报错通常是因未加入相应头文件,系统找不到相关内容。解决方法一是在报错文件中添加对应头文件,如添加ds1302.h;二是输入特定内容并在指定文件中include对应头文件,之后可直接使用对应函数,编译不再报错。

报错如下:

7a88f4d774ae4ddd9b58a28542c5d418.png

分析:出现这种情况一般就是没有加入相应头文件,导致系统找不到 xxx。

解决方法1:在报错的文件中添加相应的 #include "xxx.h"。如下,添加 ds1302.h 后再无报错。

2b4ec00af877430ea01086e6849e1bb6.png

解决方法2:

b94c933c3c784e3dab4c70f075c978c8.png

如上图,只要有输入 STM32F10X_HD,USE_STDPERIPH_DRIVER ,并在 stm32f10x_conf.h 文件中 include 对应头文件(如下图)。则在其他任意文件中无需再包含头文件,可直接使用对应的函数

e847649d2f544f6fbd15f090952fcbf1.png

编译如下,不再报错

baf4ad9f9dfd45d4bb107cb69e10de88.png

*** Using Compiler 'V5.06 update 6 (build 750)', folder: 'D:\Keil5\ARM\ARMCC\Bin' Rebuild target 'LED' assembling startup_stm32f407xx.s... compiling lcd_init.c... DEVICE\sys.h(72): error: #20: identifier "u32" is undefined void MSR_MSP(u32 addr); //设置堆栈地址 DEVICE\lcd_init.h(44): error: #20: identifier "u8" is undefined void LCD_Writ_Bus(u8 dat);//模拟SPI时序 DEVICE\lcd_init.h(45): error: #20: identifier "u8" is undefined void LCD_WR_DATA8(u8 dat);//写入一个字节 DEVICE\lcd_init.h(46): error: #20: identifier "u16" is undefined void LCD_WR_DATA(u16 dat);//写入两个字节 DEVICE\lcd_init.h(47): error: #20: identifier "u8" is undefined void LCD_WR_REG(u8 dat);//写入一个指令 DEVICE\lcd_init.h(48): error: #20: identifier "u16" is undefined void LCD_Address_Set(u16 x1,u16 y1,u16 x2,u16 y2);//设置坐标函数 DEVICE\lcd_init.h(48): error: #20: identifier "u16" is undefined void LCD_Address_Set(u16 x1,u16 y1,u16 x2,u16 y2);//设置坐标函数 DEVICE\lcd_init.h(48): error: #20: identifier "u16" is undefined void LCD_Address_Set(u16 x1,u16 y1,u16 x2,u16 y2);//设置坐标函数 DEVICE\lcd_init.h(48): error: #20: identifier "u16" is undefined void LCD_Address_Set(u16 x1,u16 y1,u16 x2,u16 y2);//设置坐标函数 DEVICE\delay.h(5): error: #20: identifier "u8" is undefined void delay_init(u8 SYSCLK); DEVICE\delay.h(6): error: #20: identifier "u16" is undefined void delay_ms(u16 nms); DEVICE\delay.h(7): error: #20: identifier "u32" is undefined void delay_us(u32 nus); DEVICE\lcd_init.c(6): error: #20: identifier "GPIO_InitTypeDef" is undefined GPIO_InitTypeDef GPIO_InitStructure; DEVICE\lcd_init.c(8): warning: #223-D: function "RCC_AHB1PeriphClockCmd" declared implicitly RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOG, ENABLE);//使能PORTA~E,PORTG时钟 DEVICE\lcd_init.c(8): error: #20: identifier "RCC_AHB1Periph_GPIOA" is undefined RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOG, ENABLE);//使能PORTA~E,PORTG时钟 DEVICE\lcd_init.c(8): error: #20: identifier "RCC_AHB1Periph_GPIOB" is undefined RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOG, ENABLE);//使能PORTA~E,PORTG时钟 DEVICE\lcd_init.c(8): error: #20: identifier "RCC_AHB1Periph_GPIOC" is undefined RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOG, ENABLE);//使能PORTA~E,PORTG时钟 DEVICE\lcd_init.c(8): error: #20: identifier "RCC_AHB1Periph_GPIOD" is undefined RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOG, ENABLE);//使能PORTA~E,PORTG时钟 DEVICE\lcd_init.c(8): error: #20: identifier "RCC_AHB1Periph_GPIOE" is undefined RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOG, ENABLE);//使能PORTA~E,PORTG时钟 DEVICE\lcd_init.c(8): error: #20: identifier "RCC_AHB1Periph_GPIOG" is undefined RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOG, ENABLE);//使能PORTA~E,PORTG时钟 DEVICE\lcd_init.c(10): error: #20: identifier "GPIO_Pin_12" is undefined GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; DEVICE\lcd_init.c(11): error: #20: identifier "GPIO_Mode_OUT" is undefined GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式 DEVICE\lcd_init.c(12): error: #20: identifier "GPIO_OType_PP" is undefined GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 DEVICE\lcd_init.c(13): error: #20: identifier "GPIO_Speed_100MHz" is undefined GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz DEVICE\lcd_init.c(14): error: #20: identifier "GPIO_PuPd_UP" is undefined GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 DEVICE\lcd_init.c(15): warning: #223-D: function "GPIO_Init" declared implicitly GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化 DEVICE\lcd_init.c(16): warning: #223-D: function "GPIO_SetBits" declared implicitly GPIO_SetBits(GPIOG,GPIO_Pin_12); DEVICE\lcd_init.c(18): error: #20: identifier "GPIO_Pin_1" is undefined GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_15 ; DEVICE\lcd_init.c(18): error: #20: identifier "GPIO_Pin_4" is undefined GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_15 ; DEVICE\lcd_init.c(18): error: #20: identifier "GPIO_Pin_5" is undefined GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_15 ; DEVICE\lcd_init.c(18): error: #20: identifier "GPIO_Pin_15" is undefined GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_15 ; DEVICE\lcd_init.c(26): error: #20: identifier "GPIO_Pin_8" is undefined GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_10; DEVICE\lcd_init.c(26): error: #20: identifier "GPIO_Pin_10" is undefined GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_10; DEVICE\lcd_init.c: 3 warnings, 30 errors compiling lcd.c... DEVICE\sys.h(72): error: #20: identifier "u32" is undefined void MSR_MSP(u32 addr); //设置堆栈地址 DEVICE\lcd.h(6): error: #20: identifier "u16" is undefined void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color);//指定区域填充颜色 DEVICE\lcd.h(6): error: #20: identifier "u16" is undefined void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color);//指定区域填充颜色 DEVICE\lcd.h(6): error: #20: identifier "u16" is undefined void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color);//指定区域填充颜色 DEVICE\lcd.h(6): error: #20: identifier "u16" is undefined void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color);//指定区域填充颜色 DEVICE\lcd.h(6): error: #20: identifier "u16" is undefined void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color);//指定区域填充颜色 DEVICE\lcd.h(7): error: #20: identifier "u16" is undefined void LCD_DrawPoint(u16 x,u16 y,u16 color);//在指定位置画一个点 DEVICE\lcd.h(7): error: #20: identifier "u16" is undefined void LCD_DrawPoint(u16 x,u16 y,u16 color);//在指定位置画一个点 DEVICE\lcd.h(7): error: #20: identifier "u16" is undefined void LCD_DrawPoint(u16 x,u16 y,u16 color);//在指定位置画一个点 DEVICE\lcd.h(8): error: #20: identifier "u16" is undefined void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color);//在指定位置画一条线 DEVICE\lcd.h(8): error: #20: identifier "u16" is undefined void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color);//在指定位置画一条线 DEVICE\lcd.h(8): error: #20: identifier "u16" is undefined void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color);//在指定位置画一条线 DEVICE\lcd.h(8): error: #20: identifier "u16" is undefined void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color);//在指定位置画一条线 DEVICE\lcd.h(8): error: #20: identifier "u16" is undefined void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color);//在指定位置画一条线 DEVICE\lcd.h(9): error: #20: identifier "u16" is undefined void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color);//在指定位置画一个矩形 DEVICE\lcd.h(9): error: #20: identifier "u16" is undefined void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color);//在指定位置画一个矩形 DEVICE\lcd.h(9): error: #20: identifier "u16" is undefined void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color);//在指定位置画一个矩形 DEVICE\lcd.h(9): error: #20: identifier "u16" is undefined void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color);//在指定位置画一个矩形 DEVICE\lcd.h(9): error: #20: identifier "u16" is undefined void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color);//在指定位置画一个矩形 DEVICE\lcd.h(10): error: #20: identifier "u16" is undefined void Draw_Circle(u16 x0,u16 y0,u8 r,u16 color);//在指定位置画一个圆 DEVICE\lcd.h(10): error: #20: identifier "u16" is undefined void Draw_Circle(u16 x0,u16 y0,u8 r,u16 color);//在指定位置画一个圆 DEVICE\lcd.h(10): error: #20: identifier "u8" is undefined void Draw_Circle(u16 x0,u16 y0,u8 r,u16 color);//在指定位置画一个圆 DEVICE\lcd.h(10): error: #20: identifier "u16" is undefined void Draw_Circle(u16 x0,u16 y0,u8 r,u16 color);//在指定位置画一个圆 DEVICE\lcd.h(12): error: #20: identifier "u16" is undefined void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//显示汉字串 DEVICE\lcd.h(12): error: #20: identifier "u16" is undefined void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//显示汉字串 DEVICE\lcd.h(12): error: #20: identifier "u8" is undefined void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//显示汉字串 DEVICE\lcd.h(12): error: #20: identifier "u16" is undefined void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//显示汉字串 DEVICE\lcd.h(12): error: #20: identifier "u16" is undefined void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//显示汉字串 DEVICE\lcd.h(12): error: #20: identifier "u8" is undefined void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//显示汉字串 DEVICE\lcd.h(12): error: #20: identifier "u8" is undefined void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//显示汉字串 DEVICE\lcd.c: 0 warnings, 30 errors compiling sys.c... DEVICE\sys.h(72): error: #20: identifier "u32" is undefined void MSR_MSP(u32 addr); //设置堆栈地址 DEVICE\sys.c(25): error: #20: identifier "u32" is undefined __asm void MSR_MSP(u32 addr) DEVICE\sys.c: 0 warnings, 2 errors compiling delay.c... .\DEVICE\sys.h(72): error: #20: identifier "u32" is undefined void MSR_MSP(u32 addr); //设置堆栈地址 DEVICE\delay.h(5): error: #20: identifier "u8" is undefined void delay_init(u8 SYSCLK); DEVICE\delay.h(6): error: #20: identifier "u16" is undefined void delay_ms(u16 nms); DEVICE\delay.h(7): error: #20: identifier "u32" is undefined void delay_us(u32 nus); DEVICE\delay.c(10): error: #20: identifier "u8" is undefined static u8 fac_us=0; //us延时倍乘数 DEVICE\delay.c(11): error: #20: identifier "u16" is undefined static u16 fac_ms=0; //ms延时倍乘数,在os下,代表每个节拍的ms数 DEVICE\delay.c(90): error: #20: identifier "u8" is undefined void delay_init(u8 SYSCLK) DEVICE\delay.c(95): warning: #223-D: function "SysTick_CLKSourceConfig" declared implicitly SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); DEVICE\delay.c(95): error: #20: identifier "SysTick_CLKSource_HCLK_Div8" is undefined SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); DEVICE\delay.c(106): error: #20: identifier "u16" is undefined fac_ms=(u16)fac_us*1000; //非OS下,代表每个ms需要的systick时钟数 DEVICE\delay.c(106): error: #65: expected a ";" fac_ms=(u16)fac_us*1000; //非OS下,代表每个ms需要的systick时钟数 DEVICE\delay.c(154): error: #20: identifier "u32" is undefined void delay_us(u32 nus) DEVICE\delay.c(156): error: #20: identifier "u32" is undefined u32 temp; DEVICE\delay.c(173): error: #20: identifier "u16" is undefined void delay_xms(u16 nms) DEVICE\delay.c(175): error: #20: identifier "u32" is undefined u32 temp; DEVICE\delay.c(176): error: #20: identifier "u32" is undefined SysTick->LOAD=(u32)nms*fac_ms; //时间加载(SysTick->LOAD为24bit) DEVICE\delay.c(176): error: #65: expected a ";" SysTick->LOAD=(u32)nms*fac_ms; //时间加载(SysTick->LOAD为24bit) DEVICE\delay.c(188): error: #20: identifier "u16" is undefined void delay_ms(u16 nms) DEVICE\delay.c(190): error: #20: identifier "u8" is undefined u8 repeat=nms/540; //这里用540,是考虑到某些客户可能超频使用, DEVICE\delay.c(192): error: #20: identifier "u16" is undefined u16 remain=nms%540; DEVICE\delay.c: 1 warning, 19 errors compiling system_stm32f4xx.c... compiling main.c... compiling stm32f4xx_hal_dma.c... compiling stm32f4xx_it.c... RTE\Device\STM32F407ZGTx\STCubeGenerated\Src\stm32f4xx_it.c(209): error: #20: identifier "UART1_Handler" is undefined HAL_UART_Transmit(&UART1_Handler, &abuffer, 1, 10);//???????? RTE\Device\STM32F407ZGTx\STCubeGenerated\Src\stm32f4xx_it.c(209): error: #20: identifier "abuffer" is undefined HAL_UART_Transmit(&UART1_Handler, &abuffer, 1, 10);//???????? RTE\Device\STM32F407ZGTx\STCubeGenerated\Src\stm32f4xx_it.c: 0 warnings, 2 errors compiling stm32f4xx_hal_cortex.c... compiling stm32f4xx_hal.c... compiling stm32f4xx_hal_tim_ex.c... compiling stm32f4xx_hal_pwr.c... compiling stm32f4xx_hal_dma_ex.c... compiling stm32f4xx_hal_pwr_ex.c... compiling stm32f4xx_hal_msp.c... compiling stm32f4xx_hal_usart.c... compiling stm32f4xx_hal_gpio.c... compiling stm32f4xx_hal_rcc_ex.c... compiling stm32f4xx_hal_uart.c... compiling stm32f4xx_hal_rcc.c... compiling stm32f4xx_hal_tim.c... ".\Objects\leddemo.axf" - 83 Error(s), 4 Warning(s). Target not created. Build Time Elapsed: 00:00:05
07-12
<think>好的,我现在需要解决用户遇到的“undefined identifier'u8'”编译错误。首先,我得确定这个错误的原因。根据经验,未定义的标识符通常是因为变量或类型没有被正确声明,或者头文件缺失,或者作用域问题。用户提到的'u8',可能是一个类型别名,比如uint8_t的缩写。但标准C库中并没有u8这个类型,除非用户自己定义了或者通过某个头文件引入了。比如在一些嵌入式系统中,可能会用typedef来定义u8为unsignedchar。这时候如果用户没有包含相应的头文件,或者没有自己定义,编译器就会报错。接下来,我需要检查用户是否包含了正确的头文件。例如,如果u8是来自<stdint.h>,那么正确的类型应该是uint8_t,而不是u8。所以用户可能混淆了这两种写法。另外,如果用户使用的是某个特定的库或框架,可能需要包含特定的头文件。另外,可能涉及到编译器对C标准的支持。比如,u8作为前缀用于UTF-8字符字面量,在C11或更高版本中,u8'c'这样的写法是合法的,但这里的u8是字面量前缀,不是类型。如果用户错误地将其用作类型,也会导致错误。 还可能用户误用了宏定义,或者存在拼写错误,比如应该是uint8_t但写成了u8。这时候需要检查代码中的类型声明部分,确认是否有正确的typedef语句。 根据引用[1]提到的,变量定义报错,需要检查变量名冲突和数据类型声明。所以这里用户需要检查是否在作用域内正确定义了u8类型,或者是否有拼写错误。此外,查看编译器报错的具体位置(行号)附近的代码,确认u8的使用是否合法。 可能的解决方案包括:1.包含正确的头文件,如<stdint.h>并使用uint8_t;2.如果使用自定义类型,添加typedef语句;3.检查拼写错误或作用域问题;4.确认编译器支持相应的C标准。另外,引用[5]提到枚举类型的问题,虽然不直接相关,但类型声明的严格性可能影响编译。需要确保所有类型都正确定义。 总结,用户需要检查u8的定义来源,确认包含必要的头文件或自定义类型,并确保拼写正确。如果是想使用标准类型,应改用uint8_t并包含<stdint.h>。</think>### 未定义标识符 'u8' 的解决方案 #### 1. **检查类型定义来源** - **标准类型**:若意图使用 `uint8_t`(无符号8位整数),需包含 `<stdint.h>` 头文件,并改用 `uint8_t` 类型: ```c #include <stdint.h> uint8_t value; // 正确写法 ``` - **自定义类型**:若项目中自定义了 `u8`(如 `typedef unsigned char u8;`),需确认定义已包含在代码中: ```c typedef unsigned char u8; // 添加自定义类型定义 u8 value; // 声明变量 ``` #### 2. **验证头文件包含** - 若 `u8` 来自第三方库(如某些嵌入式库),需检查是否包含对应的头文件: ```c #include "custom_types.h" // 示例:假设u8定义在此文件中 ``` #### 3. **编译器兼容性检查** - **UTF-8字面量前缀**:若误将 `u8` 用于字符前缀(如 `u8'c'`),需注意其用途与类型无关: ```c char utf8_char = u8'c'; // C11及以上支持,但仅用于字符/字符串前缀 ``` #### 4. **排查拼写与作用域问题** - 检查变量声明位置是否在作用域内(如局部变量需先声明后使用)。 - 确认代码中无拼写错误(如 `u8` 写成 `u9` 或 `U8`)。 #### 5. **修复示例** ```c // 修复前(错误) u8 data; // 报错:undefined identifier 'u8' // 修复后(正确方式1:使用标准类型) #include <stdint.h> uint8_t data; // 修复后(正确方式2:自定义类型) typedef unsigned char u8; u8 data; ``` --- ### 相关问题 1. 如何避免C语言中的未定义类型错误? 2. `<stdint.h>` 包含哪些常用数据类型? 3. 编译器报“undefined identifier”时,如何快速定位问题? --- ### 引用说明 - 若 `u8` 是项目自定义类型,需确保其定义在全局作用域且无命名冲突[^1]。 - 第三方库中的类型需通过头文件正确引入,否则会触发未定义错误[^2]。 - 编译器对C标准的支持差异可能导致语法误解(如 `u8` 前缀仅在C11后有效)[^3]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值