stm32_之【errors】重定义错误 Symbol Key_GPIO_Config multiply defined (by key.o and main.o).

本文介绍了一种常见的C语言编程错误——函数重复定义,并提供了详细的解决方案。通过正确使用头文件(.h)来声明函数,而非直接包含源文件(.c),避免了编译时的错误。

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

编译失败:在轮询点灯中,编写完main.c、led.c、key.c文件之后,进行最后一步,编译工程,出现如下错误,在编译器编译key.c时输出的key.o文件和编译main.c时输出的main.o文件中,有函数重复定义了,分别是:delay_ms()延时函数、Key_GPIO_Config()按键引脚配置函数、KeyScan()按键扫描函数:

 

 

发现错因:在main.c中include了key.c文件

 

尝试解决:通过问郭工知道了,在导入库时不能include XXX.c 文件,这样做等价于把key.c整篇复制到main.c中,于是,我在main.c中把#include"key.c"这行代码删除,观察编译结果,是否去除了多了的函数,发现很多东西没有定义(未定义)

 

 

 

进一步检查错因:

没有编写key.h文件,无法与main.c进行include库链接

因为main.c中要调用这三个函数,即:delay_ms()延时函数、Key_GPIO_Config()按键引脚配置函数、KeyScan()按键扫描函数。调用函数前要定义函数,很显然这三个函数定义在key.c中,已经确定了函数定义的位置,然后要把三个函数调用到另一个C文件即main.c中,就要声明函数,那么应该在哪里声明函数呢?

应该在定义函数的key.c文件对应的key.h文件声明函数、声明常量,比如KEY_OFF置1都应该写在key.h文件中。然后再在目的地main.c中#include key.h文件,就能将main.c编译成功,不会出现重定义,也不会出现未定义了。

 

1.函数定义处:

 

2.函数声明处:

 

3.函数调用处:

 

编写key.h文件

#ifndef  __文件名_H_

#define  __文件名_H_



#define  要调用的.c中的常量  对应值

extern void 要调用的。c中的函数(void);



#endif

 

 

 

 

 

Rebuild started: Project: AD9959 *** Using Compiler 'V5.06 update 7 (build 960)', folder: 'F:\stc\ARM\ARMCC\Bin' Rebuild target '2024C' Note: source file '..\Core\Src\gpio.c' - object file renamed from 'AD9959\gpio.o' to 'AD9959\gpio_1.o'. Note: source file '..\Core\Src\main.c' - object file renamed from 'AD9959\main.o' to 'AD9959\main_1.o'. Note: source file '..\Core\Src\stm32f4xx_hal_msp.c' - object file renamed from 'AD9959\stm32f4xx_hal_msp.o' to 'AD9959\stm32f4xx_hal_msp_1.o'. Note: source file '..\Core\Src\stm32f4xx_it.c' - object file renamed from 'AD9959\stm32f4xx_it.o' to 'AD9959\stm32f4xx_it_1.o'. Note: source file '..\Core\Src\system_stm32f4xx.c' - object file renamed from 'AD9959\system_stm32f4xx.o' to 'AD9959\system_stm32f4xx_1.o'. assembling startup_stm32f407xx.s... compiling stm32f4xx_hal_flash_ramfunc.c... compiling gpio.c... compiling stm32f4xx_hal_dma_ex.c... compiling system_stm32f4xx.c... compiling stm32f4xx_hal_cortex.c... compiling stm32f4xx_hal_pwr.c... compiling stm32f4xx_hal_pwr_ex.c... compiling stm32f4xx_hal_tim_ex.c... compiling Delay.c... compiling stm32f4xx_hal_exti.c... compiling screen.c... compiling stm32f4xx_hal_msp.c... compiling stm32f4xx_hal_gpio.c... compiling stm32f4xx_hal.c... compiling ad9959.c... compiling stm32f4xx_hal_flash.c... compiling stm32f4xx_hal_rcc_ex.c... compiling stm32f4xx_it.c... compiling stm32f4xx_hal_flash_ex.c... compiling key.c... ..\Hardware\key.c(59): warning: #1-D: last line of file ends without a newline ..\Hardware\key.c: 1 warning, 0 errors compiling main.c... compiling stm32f4xx_hal_dma.c... compiling stm32f4xx_hal_rcc.c... compiling stm32f4xx_hal_tim.c... compiling sys.c... compiling uart.c... compiling gpio.c... compiling main.c... compiling stm32f4xx_hal_msp.c... compiling system_stm32f4xx.c... compiling stm32f4xx_it.c... linking... AD9959\AD9959.axf: Error: L6200E: Symbol SystemInit multiply defined (by system_stm32f4xx_1.o and system_stm32f4xx.o). AD9959\AD9959.axf: Error: L6200E: Symbol NMI_Handler multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol HardFault_Handler multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol MemManage_Handler multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol BusFault_Handler multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol UsageFault_Handler multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol SVC_Handler multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol DebugMon_Handler multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol PendSV_Handler multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol SysTick_Handler multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___6_main_c_66f8bec5____REV16 multiply defined (by main_1.o and main.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___6_main_c_66f8bec5____REVSH multiply defined (by main_1.o and main.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___6_main_c_66f8bec5____RRX multiply defined (by main_1.o and main.o). AD9959\AD9959.axf: Error: L6200E: Symbol __ARM_use_no_argv multiply defined (by main_1.o and main.o). AD9959\AD9959.axf: Error: L6200E: Symbol main multiply defined (by main_1.o and main.o). AD9959\AD9959.axf: Error: L6200E: Symbol screen_Init multiply defined (by main_1.o and main.o). AD9959\AD9959.axf: Error: L6200E: Symbol signal_setting multiply defined (by main_1.o and main.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___6_gpio_c_62724882____REV16 multiply defined (by gpio_1.o and gpio.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___6_gpio_c_62724882____REVSH multiply defined (by gpio_1.o and gpio.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___6_gpio_c_62724882____RRX multiply defined (by gpio_1.o and gpio.o). AD9959\AD9959.axf: Error: L6200E: Symbol MX_GPIO_Init multiply defined (by gpio_1.o and gpio.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___14_stm32f4xx_it_c_bb8ca80c____REV16 multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___14_stm32f4xx_it_c_bb8ca80c____REVSH multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___14_stm32f4xx_it_c_bb8ca80c____RRX multiply defined (by stm32f4xx_it_1.o and stm32f4xx_it.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___19_stm32f4xx_hal_msp_c_d46e2bee____REV16 multiply defined (by stm32f4xx_hal_msp_1.o and stm32f4xx_hal_msp.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___19_stm32f4xx_hal_msp_c_d46e2bee____REVSH multiply defined (by stm32f4xx_hal_msp_1.o and stm32f4xx_hal_msp.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___19_stm32f4xx_hal_msp_c_d46e2bee____RRX multiply defined (by stm32f4xx_hal_msp_1.o and stm32f4xx_hal_msp.o). AD9959\AD9959.axf: Error: L6200E: Symbol HAL_MspInit multiply defined (by stm32f4xx_hal_msp_1.o and stm32f4xx_hal_msp.o). AD9959\AD9959.axf: Error: L6200E: Symbol SystemCoreClock multiply defined (by system_stm32f4xx_1.o and system_stm32f4xx.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___18_system_stm32f4xx_c_5d646a67____REV16 multiply defined (by system_stm32f4xx_1.o and system_stm32f4xx.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___18_system_stm32f4xx_c_5d646a67____REVSH multiply defined (by system_stm32f4xx_1.o and system_stm32f4xx.o). AD9959\AD9959.axf: Error: L6200E: Symbol __asm___18_system_stm32f4xx_c_5d646a67____RRX multiply defined (by system_stm32f4xx_1.o and system_stm32f4xx.o). AD9959\AD9959.axf: Error: L6200E: Symbol AHBPrescTable multiply defined (by system_stm32f4xx_1.o and system_stm32f4xx.o). AD9959\AD9959.axf: Error: L6200E: Symbol APBPrescTable multiply defined (by system_stm32f4xx_1.o and system_stm32f4xx.o). AD9959\AD9959.axf: Error: L6200E: Symbol SystemCoreClockUpdate multiply defined (by system_stm32f4xx_1.o and system_stm32f4xx.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 35 error messages. "AD9959\AD9959.axf" - 35 Error(s), 1 Warning(s). Target not created. Build Time Elapsed: 00:00:07
06-03
*** Using Compiler 'V5.06 update 5 (build 528)', folder: 'D:\Keil_v5\ARM\ARMCC\Bin' Build target 'APP' compiling main.c... compiling dma.c... compiling gpio.c... compiling stm32f1xx_it.c... compiling tim.c... compiling stm32f1xx_hal_msp.c... compiling usart.c... compiling logic.c... compiling uart_app.c... compiling system.c... compiling periph.c... compiling schedule.c... ..\APP\schedule.c(64): warning: #1-D: last line of file ends without a newline ..\APP\schedule.c: 1 warning, 0 errors linking... APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by gpio.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by dma.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by tim.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by usart.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by stm32f1xx_it.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by stm32f1xx_hal_msp.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by logic.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by periph.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by schedule.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by system.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_length multiply defined (by uart_app.o and main.o). APP\APP.axf: Error: L6200E: Symbol uart_proc multiply defined (by uart_app.o and stm32f1xx_it.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 12 error messages. "APP\APP.axf" - 12 Error(s), 1 Warning(s). Target not created. Build Time Elapsed: 00:00:09
最新发布
07-20
*** Using Compiler 'V5.06 update 6 (build 750)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' Rebuild target 'BUZZERWorking2' assembling startup_stm32f103xe.s... compiling main.c... ../Inc/main.h(57): warning: #1295-D: Deprecated declaration USER_UART_IRQHandler - give arg types void USER_UART_IRQHandler(); ../Src/main.c: 1 warning, 0 errors compiling gpio.c... ../Inc/main.h(57): warning: #1295-D: Deprecated declaration USER_UART_IRQHandler - give arg types void USER_UART_IRQHandler(); ../Src/gpio.c: 1 warning, 0 errors compiling stm32f1xx_hal_gpio_ex.c... compiling stm32f1xx_hal_tim.c... compiling stm32f1xx_hal_msp.c... ../Inc/main.h(57): warning: #1295-D: Deprecated declaration USER_UART_IRQHandler - give arg types void USER_UART_IRQHandler(); ../Src/stm32f1xx_hal_msp.c: 1 warning, 0 errors compiling stm32f1xx_it.c... ../Inc/main.h(57): warning: #1295-D: Deprecated declaration USER_UART_IRQHandler - give arg types void USER_UART_IRQHandler(); ../Src/stm32f1xx_it.c: 1 warning, 0 errors compiling stm32f1xx_hal_tim_ex.c... compiling stm32f1xx_hal.c... compiling stm32f1xx_hal_rcc_ex.c... compiling stm32f1xx_hal_rcc.c... compiling stm32f1xx_hal_cortex.c... compiling stm32f1xx_hal_dma.c... compiling stm32f1xx_hal_pwr.c... compiling stm32f1xx_hal_gpio.c... compiling stm32f1xx_hal_flash.c... compiling system_stm32f1xx.c... compiling stm32f1xx_hal_exti.c... compiling stm32f1xx_hal_flash_ex.c... linking... BUZZERWorking2\BUZZERWorking2.axf: Error: L6200E: Symbol MX_GPIO_Init multiply defined (by gpio.o and main.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 1 error messages. "BUZZERWorking2\BUZZERWorking2.axf" - 1 Error(s), 4 Warning(s). Target not created. Build Time Elapsed: 00:00:10
07-05
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值