错误“…的多重定义首先在此处定义”

问题描述 投票:0回答:1

我使用的是 PIC32MM0256GPM048-i,带有 MPLAB X IDE v6.10、编译器 XC32 v4.30。我还使用 OPT3101 光学传感器,它通过 I2C 与 PIC 进行通信。我面临这个错误:

c:\program files\microchip\xc32\v4.30\bin\bin\gcc\pic32mx\8.3.1........\bin/pic32m-ld.exe: build/default/production/src/opt3101.o:(.rodata+0x0): multiple definition of `registerToEeprom'; build/default/production/senstrolibc/src/OPT/i2cOpt.o:(.rodata+0x0): first defined here

我有以下文件:

opt3101.c,
opt3101.h,
i2cOpt.c,
i2cOpt.h and
i2cOptConfig.h

以下是有用的代码片段:

opt31010.c:

#include <xc.h>
#include "system.h"
#include "pin_manager.h"
#include "opt3101.h"
#include "i2cOpt.h"

然后我只有函数定义,特别是

void OptInit(void)
,它使用有问题的变量
registerToEeprom

opt3101.h:

#ifndef OPT_3101
#define OPT_3101

// macros, functions declarations

#endif /* OPT_3101 */

i2cOpt.c:

#include <xc.h>
#include "system.h"
#include "i2cOpt.h"
#include "i2c1.h"
#include "i2c2.h"

#ifndef OPT_REFERENCE_3101
#define OPT_REFERENCE_3101
#endif

然后我就有了我的函数定义。

i2cOpt.h:

#ifndef I2C_OPT_H
#define I2C_OPT_H

#include "i2cOptConfig.h"

// functions declarations

#endif /* I2C_OPT_H */

i2cOptConfig:

#ifndef I2C_OPT_CONFIG_H
#define I2C_OPT_CONFIG_H

// macros, including NB_OF_INIT_REGISTERS

typedef struct 
{
    uint8_t registerAddress;
    uint8_t eepromAddress;
} address_s;

const address_s registerToEeprom[NB_OF_INIT_REGISTERS] = 
{
    {0x0B, 0x65},
    {0x0F, 0x0E},
    {0x10, 0x00},
    {0x13, 0x00},
    {0x14, 0x00} 
};

#endif /* I2C_OPT_CONFIG_H */

我真的不明白为什么会出现这个错误。当然,我已经四处搜索,但其他人的错误是由于标头内的函数定义造成的,这不是我的情况。 此外,我确实在标题中包含了守卫,并且它们彼此不同。

我也尝试过这个:i2cOptConfig中的

extern const address_s registerToEeprom[NB_OF_INIT_REGISTERS];
,在i2cOpt.h中初始化
registerToEeprom
,但它也不起作用。

有人可以帮助我吗?

arrays c include extern pic32
1个回答
0
投票

您在头文件中有变量

registerToEeprom
的定义。这意味着包含该标头的任何 .c 文件都具有该变量的定义。当您在编译每个 .c 文件后链接程序时,您会收到多重定义错误。

将定义移动到不同的头文件并没有帮助,因为您仍然遇到在多个源文件包含的标头中定义变量的相同问题。

extern
声明放入头文件中,然后将定义放入一个源文件中,可能是 i2cOpt.c。

© www.soinside.com 2019 - 2024. All rights reserved.