编译时也许会告诉你重复定义分别出现在哪里。
需要注意的是,如果把变量移动到某个.h文件里,可能会导致有多个.c文件引用该头文件,进而产生多个该变量的定义,所以C语言里有要求,定义变量在.c里,声明在.h里。定义和声明用是否有extern来区分,有extern的是声明,多次出现也无所谓;没有extern的是定义,这个只能出现一次
错误写法
.h 里面写这个
extern const unsigned int FLASH_ADDRESS_BUF[ = {\
0x0800A000,0x0800A400,0x0800A800,0x0800AC00,0x0800B000,0x0800B400,0x0800B800,0x0800BC00,\
0x0800C000,0x0800C400,0x0800C800,0x0800CC00,0x0800D000,0x0800D400,0x0800D800,0x0800DC00,\
0x0800E000,0x0800E400,0x0800E800,0x0800EC00,0x0800F000,0x0800F400,0x0800F800,0x0800FC00,\
0x08010000,0x08010400,0x08010800,0x08010C00,0x08011000,0x08011400,0x08011800,0x08011C00,\
0x08012000,0x08012400,0x08012800,0x08012C00,0x08013000,0x08013400,0x08013800,0x08013C00,\
0x08014000,0x08014400,0x08014800,0x08014C00,0x08015000,0x08015400,0x08015800,0x08015C00,\
0x08016000,0x08016400,0x08016800,0x08016C00,0x08017000,0x08017400,0x08017800,0x08017C00,\
0x08018000,0x08018400,0x08018800,0x08018C00,0x08019000,0x08019400,0x08019800,0x08019C00,\
0x0801A000,0x0801A400,0x0801A800,0x0801AC00,0x0801B000,0x0801B400,0x0801B800,0x0801BC00,\
0x0801C000,0x0801C400,0x0801C800,0x0801CC00,0x0801D000,0x0801D400,0x0801D800,0x0801DC00,\
0x0801E000,0x0801E400,0x0801E800,0x0801EC00,0x0801F000,0x0801F400,0x0801F800,0x0801FC00};];
extern unsigned char Sector_Count; //用于读写扇区地址选择,指向FLASH_ADDRESS_BUF的地址
.c里面写这个
const unsigned int FLASH_ADDRESS_BUF[128-40];
unsigned char Sector_Count = 0; //用于读写扇区地址选择,指向FLASH_ADDRESS_BUF的地址
修改成
.h写这个
extern const unsigned int FLASH_ADDRESS_BUF[128 - 40];
extern unsigned char Sector_Count; //用于读写扇区地址选择,指向FLASH_ADDRESS_BUF的地址
.c写这个
const unsigned int FLASH_ADDRESS_BUF[] = {\
0x0800A000,0x0800A400,0x0800A800,0x0800AC00,0x0800B000,0x0800B400,0x0800B800,0x0800BC00,\
0x0800C000,0x0800C400,0x0800C800,0x0800CC00,0x0800D000,0x0800D400,0x0800D800,0x0800DC00,\
0x0800E000,0x0800E400,0x0800E800,0x0800EC00,0x0800F000,0x0800F400,0x0800F800,0x0800FC00,\
0x08010000,0x08010400,0x08010800,0x08010C00,0x08011000,0x08011400,0x08011800,0x08011C00,\
0x08012000,0x08012400,0x08012800,0x08012C00,0x08013000,0x08013400,0x08013800,0x08013C00,\
0x08014000,0x08014400,0x08014800,0x08014C00,0x08015000,0x08015400,0x08015800,0x08015C00,\
0x08016000,0x08016400,0x08016800,0x08016C00,0x08017000,0x08017400,0x08017800,0x08017C00,\
0x08018000,0x08018400,0x08018800,0x08018C00,0x08019000,0x08019400,0x08019800,0x08019C00,\
0x0801A000,0x0801A400,0x0801A800,0x0801AC00,0x0801B000,0x0801B400,0x0801B800,0x0801BC00,\
0x0801C000,0x0801C400,0x0801C800,0x0801CC00,0x0801D000,0x0801D400,0x0801D800,0x0801DC00,\
0x0801E000,0x0801E400,0x0801E800,0x0801EC00,0x0801F000,0x0801F400,0x0801F800,0x0801FC00};;
unsigned char Sector_Count = 0; //用于读写扇区地址选择,指向FLASH_ADDRESS_BUF的地址
————————————————
版权声明:本文为CSDN博主「明东(Maxwell)」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Maxwell_321/article/details/120684447
|