error:variable “diff” was set but never used

在编译TI的SDK过程中遇到高查找错误级别问题,通过调整编译选项和使用volatile关键字解决了未使用变量被优化导致的编译错误。文章详细介绍了问题的根源及解决方案。

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

最近在编译TI的SDK,但是它的查找错误级别很高,吧一些没有必要的警告都变成错误,我想到的办法第一个就是应该是gcc编译的时候加上了  -Werror,但是整个SDK实在太大,很多地方都有,我去掉了-werror后还是有错,所以我选择了用volatile将变量设置为易变的,救过是成功编译,结论:编译器将未使用的变量进行了优化,优化后去掉了次变量,但是函数里面有部分比如打印有用到,导致了找不到此变量。所以用volatile设置变量,编译器就无法将它优化掉。

 

也有可能是这个选项导致的,--emit_warnings_as_errors,大致的含义就是把警告输出为错误。

*** Using Compiler 'V5.06 update 5 (build 528)', folder: 'E:\Keil5\ARM\ARMCC\Bin' Build target 'OLED' compiling stm32f4xx_ll_adc.c... compiling stm32f4xx_hal_pwr.c... assembling startup_stm32f411xe.s... compiling oled.c... ..\Core\Src\oled.c(23): error: #20: identifier "OLED_DC_GPIO_Port" is undefined LED_DC_Set();//命令/数据标志位置为1,则表示传送的是命令字节 ..\Core\Src\oled.c(23): error: #20: identifier "OLED_DC_Pin" is undefined LED_DC_Set();//命令/数据标志位置为1,则表示传送的是命令字节 ..\Core\Src\oled.c(26): error: #20: identifier "OLED_DC_GPIO_Port" is undefined LED_DC_Clr();//命令/数据标志位置为0,则表示传送的是数据字节 ..\Core\Src\oled.c(26): error: #20: identifier "OLED_DC_Pin" is undefined LED_DC_Clr();//命令/数据标志位置为0,则表示传送的是数据字节 ..\Core\Src\oled.c(27): error: #20: identifier "OLED_CS_GPIO_Port" is undefined LED_CS_Clr();//片选信号为低,表示选中OLED ..\Core\Src\oled.c(27): error: #20: identifier "OLED_CS_Pin" is undefined LED_CS_Clr();//片选信号为低,表示选中OLED ..\Core\Src\oled.c(30): error: #20: identifier "OLED_DC_GPIO_Port" is undefined LED_DC_Set(); ..\Core\Src\oled.c(30): error: #20: identifier "OLED_DC_Pin" is undefined LED_DC_Set(); ..\Core\Src\oled.c(243): error: #20: identifier "OLED_RST_GPIO_Port" is undefined LED_RST_Clr(); ..\Core\Src\oled.c(243): error: #20: identifier "OLED_RST_Pin" is undefined LED_RST_Clr(); ..\Core\Src\oled.c: 0 warnings, 10 errors compiling gpio.c... ../Core/Src/gpio.c(96): error: #20: identifier "OLED_CS_GPIO_Port" is undefined HAL_GPIO_WritePin(OLED_CS_GPIO_Port, OLED_CS_Pin, GPIO_PIN_RESET); ../Core/Src/gpio.c(96): error: #20: identifier "OLED_CS_Pin" is undefined HAL_GPIO_WritePin(OLED_CS_GPIO_Port, OLED_CS_Pin, GPIO_PIN_RESET); ../Core/Src/gpio.c(99): error: #20: identifier "OLED_RST_Pin" is undefined HAL_GPIO_WritePin( PIOB, OLED_RST_Pin|OLED_DC_Pin, GPIO_PIN_RESET); ../Core/Src/gpio.c(99): error: #20: identifier "OLED_DC_Pin" is undefined HAL_GPIO_WritePin( PIOB, OLED_RST_Pin|OLED_DC_Pin, GPIO_PIN_RESET); ../Core/Src/gpio.c: 0 warnings, 4 errors compiling i2c.c... ../Core/Inc/i2c.h(34): error: #20: identifier "I2C_HandleTypeDef" is undefined extern I2C_HandleTypeDef hi2c1; i2c.c(27): error: #20: identifier "I2C_HandleTypeDef" is undefined I2C_HandleTypeDef hi2c1; i2c.c(35): error: #20: identifier "I2C_DUTYCYCLE_2" is undefined hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; i2c.c(37): error: #20: identifier "I2C_ADDRESSINGMODE_7BIT" is undefined hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; i2c.c(38): error: #20: identifier "I2C_DUALADDRESS_DISABLE" is undefined hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; i2c.c(40): error: #20: identifier "I2C_GENERALCALL_DISABLE" is undefined hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; i2c.c(41): error: #20: identifier "I2C_NOSTRETCH_DISABLE" is undefined hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; i2c.c(42): warning: #223-D: function "HAL_I2C_Init" declared implicitly if (HAL_I2C_Init(&hi2c1) != HAL_OK) i2c.c(49): error: #20: identifier "I2C_HandleTypeDef" is undefined void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) i2c.c(79): error: #20: identifier "I2C_HandleTypeDef" is undefined void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) i2c.c(108): warning: #1-D: last line of file ends without a newline /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ i2c.c: 2 warnings, 9 errors compiling stm32f4xx_hal_rtc_ex.c... compiling stm32f4xx_hal_cortex.c... compiling stm32f4xx_hal_spi.c... compiling stm32f4xx_hal_flash_ramfunc.c... compiling stm32f4xx_hal.c... compiling stm32f4xx_hal_adc.c... compiling tim.c... compiling stm32f4xx_hal_dma_ex.c... compiling usart.c... compiling stm32f4xx_hal_flash.c... compiling stm32f4xx_hal_rcc.c... compiling adc.c... compiling stm32f4xx_hal_exti.c... compiling main.c... ../Core/Src/main.c(289): warning: #177-D: variable "i" was declared but never referenced int i,j; ../Core/Src/main.c(371): warning: #1035-D: single-precision operand implicitly converted to double-precision Res_1 = ( ( (float)adc_values[1] ) / ( (float)adc_values[0] )-1.0)*708; ../Core/Src/main.c(375): warning: #1035-D: single-precision operand implicitly converted to double-precision Res_2 = (int)(( ( (float)adc_values[1] ) / ( (float)adc_values[0] )-1.0)*475000); ../Core/Src/main.c(503): warning: #1035-D: single-precision operand implicitly converted to double-precision Esr = ( ( (float)adc_values[1] ) / ( (float)adc_values[0] )-1.0)*708; ../Core/Src/main.c(489): warning: #550-D: variable "adcget1" was set but never used uint32_t adcget1, adcget2, adc_values[2]; ../Core/Src/main.c(489): warning: #550-D: variable "adcget2" was set but never used uint32_t adcget1, adcget2, adc_values[2]; ../Core/Src/main.c(490): warning: #177-D: variable "Res" was declared but never referenced float Res,Esr; ../Core/Src/main.c: 7 warnings, 0 errors compiling spi.c... compiling stm32f4xx_it.c... compiling stm32f4xx_hal_dma.c... compiling stm32f4xx_hal_pwr_ex.c... compiling stm32f4xx_hal_rtc.c... compiling stm32f4xx_hal_gpio.c... compiling stm32f4xx_hal_rcc_ex.c... compiling stm32f4xx_hal_flash_ex.c... compiling stm32f4xx_hal_adc_ex.c... compiling stm32f4xx_hal_msp.c... compiling rtc.c... compiling stm32f4xx_hal_tim_ex.c... compiling stm32f4xx_hal_tim.c... compiling stm32f4xx_hal_uart.c... compiling system_stm32f4xx.c... "OLED\OLED.axf" - 23 Error(s), 9 Warning(s). Target not created. Build Time Elapsed: 00:02:10
最新发布
07-21
“”/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2025 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "cmsis_os.h" #include "can.h" #include "dma.h" #include "usart.h" #include "usb_otg.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); void MX_FREERTOS_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_CAN1_Init(); MX_USART1_UART_Init(); MX_USART3_UART_Init(); MX_USART6_UART_Init(); MX_USB_OTG_FS_PCD_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Call init function for freertos objects (in cmsis_os2.c) */ MX_FREERTOS_Init(); /* Start scheduler */ osKernelStart(); /* We should never get here as control is now taken by the scheduler */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 25; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 7; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */“*** Using Compiler 'V5.06 update 5 (build 528)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' Build target 'Can_Serial' compiling chassis_task.c... ..\TASK\chassis_task.c(96): warning: #223-D: function "osDelay" declared implicitly osDelay(CHASSIS_TASK_INIT_TIME); ..\TASK\chassis_task.c(99): warning: #550-D: variable "lastWakeTime" was set but never used static TickType_t lastWakeTime; ..\TASK\chassis_task.c(264): warning: #1035-D: single-precision operand implicitly converted to double-precision chassis_move.vx_set=-(fp32)speedx*0.4; ..\TASK\chassis_task.c(265): warning: #1035-D: single-precision operand implicitly converted to double-precision chassis_move.vy_set=-(fp32)speedy*0.4; ..\TASK\chassis_task.c(269): warning: #1035-D: single-precision operand implicitly converted to double-precision chassis_move.wz_set= chassis_move.wz_set*1.2; ..\TASK\chassis_task.c: 5 warnings, 0 errors linking... Can_Serial\Can_Serial.axf: Error: L6218E: Undefined symbol MX_USB_OTG_FS_PCD_Init (referred from main.o). Can_Serial\Can_Serial.axf: Error: L6218E: Undefined symbol chassis_control_loop (referred from chassis_task.o). Not enough information to list image symbols. Not enough information to list load addresses in the image map. Finished: 2 information, 0 warning and 2 error messages. "Can_Serial\Can_Serial.axf" - 2 Error(s), 5 Warning(s). Target not created. Build Time Elapsed: 00:00:03”
06-27
#include "stm32f4xx.h" #include "stm32f4xx_adc.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_rcc.h" // 系统时钟配置 (168MHz HSE) void SystemClock_Config(void) { RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); RCC_PLLConfig(RCC_PLLSource_HSE, 8, 336, 2, 7); // PLLM=8, PLLN=336, PLLP=2, PLLQ=7 RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div4); RCC_PCLK2Config(RCC_HCLK_Div2); } // ADC初始化函数修正 void ADC1_Init(void) { // 修改函数名避免冲突 ADC_InitTypeDef ADC_InitStruct = {0}; ADC_CommonInitTypeDef ADC_CommonInitStruct = {0}; // 启用时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 配置GPIO GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStruct); // ADC公共配置 ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div4; ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStruct); // ADC独立配置 ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; ADC_InitStruct.ADC_ScanConvMode = DISABLE; ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // 修正参数名 ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStruct); // 配置规则通道 ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles); ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConv(ADC1); } // GPIO初始化 (PD15控制继电器) void Relay_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // 降低速度避免过冲 GPIO_Init(GPIOD, &GPIO_InitStruct); } // ADC均值读取函数(避免变量未使用警告) uint16_t ADC_ReadAverage(uint8_t samples) { uint32_t sum = 0; for(uint8_t i = 0; i < samples; i++) { while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); sum += ADC_GetConversionValue(ADC1); } return (uint16_t)(sum / samples); } // 滞回比较控制 void PumpControl(void) { #define WATER_THRESHOLD 1500 // 12位ADC阈值 #define HYSTERESIS 50 // 滞回区间 static uint8_t pumpState = 0; // 使用 变量避免警告 uint16_t adc_value = ADC_ReadAverage(10); if (adc_value > (WATER_THRESHOLD + HYSTERESIS)) { GPIO_ResetBits(GPIOD, GPIO_Pin_15); // 关水泵 pumpState = 0; // 更新状态 } else if (adc_value < (WATER_THRESHOLD - HYSTERESIS)) { GPIO_SetBits(GPIOD, GPIO_Pin_15); // 开水泵 pumpState = 1; // 更新状态 } } int main(void) { SystemClock_Config(); ADC1_Init(); // 使用修正后的函数名 Relay_GPIO_Init(); while(1) { PumpControl(); for(volatile uint32_t i = 0; i < 500000; i++); // 消除变量未使用警告 } } *** Using Compiler 'V5.06 update 6 (build 750)', folder: 'D:\stm32\软件\ARM\ARMCC\Bin' compiling main.c... main.c(87): warning: #550-D: variable "pumpState" was set but never used static uint8_t pumpState = 0; // 使用 变量避免警告 main.c: 1 warning, 0 errors "main.c" - 0 Error(s), 1 Warning(s).进行修改
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值