关于const,关于STM32,关于multiply defined symbols的错误。

本文探讨了在C语言中,由于const全局变量在多个源文件中被重复定义而导致的链接错误。通过解释const与static连结属性的关系,提供了解决方案,即在header文件中使用static const来避免multiply defined symbols错误。

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

师姐调试STM32的一个工程,程序出了小bug。。小弟看来看去,没逻辑上的问题,嗯,想想还是应该这个问题,果然。

在C中,定义一个const形对象是会分配空间的,并且这个空间具有全局域,因此如果一个const定义在header中,那么如果这个header被多个c源文件包含的话,就会产生multiply   defined   symbols的错误。 可以通过增加static的方式将它的连结属性变成局部的就可以了。  在C++中,const默认具有static连结域,因此在C++中可以在header中 定义const。

如下:

在arm_const_structs.h中修改。

static  const arm_cfft_instance_f32 arm_cfft_sR_f32_len1024 = {
      1024, twiddleCoef_1024, armBitRevIndexTable1024, ARMBITREVINDEXTABLE1024_TABLE_LENGTH
   };

在main.c中插入。

extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len1024; 

太久没写嵌入式代码,平时还是得温习温习。
   
 

*** Using Compiler 'V5.06 update 5 (build 528)', folder: 'D:\Keil_v5\ARM\ARMCC\Bin' Build target 'APP' compiling main.c... linking... APP\APP.axf: Error: L6200E: Symbol rx_buf multiply defined (by usart.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_buf multiply defined (by stm32f1xx_it.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_buf multiply defined (by logic.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_buf multiply defined (by periph.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_buf multiply defined (by schedule.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_buf multiply defined (by system.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_byte multiply defined (by usart.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_byte multiply defined (by stm32f1xx_it.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_byte multiply defined (by logic.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_byte multiply defined (by periph.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_byte multiply defined (by schedule.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_byte multiply defined (by system.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_flag multiply defined (by usart.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_flag multiply defined (by stm32f1xx_it.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_flag multiply defined (by logic.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_flag multiply defined (by periph.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_flag multiply defined (by schedule.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_flag multiply defined (by system.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_count multiply defined (by usart.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_count multiply defined (by stm32f1xx_it.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_count multiply defined (by logic.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_count multiply defined (by periph.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_count multiply defined (by schedule.o and main.o). APP\APP.axf: Error: L6200E: Symbol timeout_count multiply defined (by system.o and main.o). APP\APP.axf: Error: L6200E: Symbol uart_rx_complete multiply defined (by usart.o and main.o). APP\APP.axf: Error: L6200E: Symbol uart_rx_complete multiply defined (by stm32f1xx_it.o and main.o). APP\APP.axf: Error: L6200E: Symbol uart_rx_complete multiply defined (by logic.o and main.o). APP\APP.axf: Error: L6200E: Symbol uart_rx_complete multiply defined (by periph.o and main.o). APP\APP.axf: Error: L6200E: Symbol uart_rx_complete multiply defined (by schedule.o and main.o). APP\APP.axf: Error: L6200E: Symbol uart_rx_complete multiply defined (by system.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_index multiply defined (by usart.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_index multiply defined (by stm32f1xx_it.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_index multiply defined (by logic.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_index multiply defined (by periph.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_index multiply defined (by schedule.o and main.o). APP\APP.axf: Error: L6200E: Symbol rx_index multiply defined (by system.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 36 error messages. "APP\APP.axf" - 36 Error(s), 0 Warning(s). Target not created. Build Time Elapsed: 00:00:04
最新发布
07-22
linking... ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by mh2203_it.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by init.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by bsp_i2c.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by bsp_eeprom.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by bsp_buzzer.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by bsp_ds1302.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by bsp_ht1621.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by bsp_lcd.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by lcd_fd_1018.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by hw_config.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by usb_endp.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by mcu_cfg.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by timer.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_1 multiply defined (by key.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by mh2203_it.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by init.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by bsp_i2c.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by bsp_eeprom.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by bsp_buzzer.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by bsp_ds1302.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by bsp_ht1621.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by bsp_lcd.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by lcd_fd_1018.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by hw_config.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by usb_endp.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by mcu_cfg.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by timer.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_2 multiply defined (by key.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by mh2203_it.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by init.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by bsp_i2c.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by bsp_eeprom.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by bsp_buzzer.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by bsp_ds1302.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by bsp_ht1621.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by bsp_lcd.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by lcd_fd_1018.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by hw_config.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by usb_endp.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by mcu_cfg.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by timer.o and main.o). ..\Output\SW_572.axf: Error: L6200E: Symbol ch_3 multiply defined (by key.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 42 error messages. "..\Output\SW_572.axf" - 42 Error(s), 3 Warning(s).
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值