arrayscincludeexternpic32

Error "multiple definition of ... first defined here"


I'm using a PIC32MM0256GPM048-i, with MPLAB X IDE v6.10, compiler XC32 v4.30. I'm also using an OPT3101 optical sensor, that commmunicates through I2C with the PIC. I'm facing this error:

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

I have the following files: opt3101.c, opt3101.h, i2cOpt.c, i2cOpt.h and i2cOptConfig.h

Here are the code snippets that will be useful:

opt31010.c:

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

Then I just have my functions definitions, notably void OptInit(void) which uses the problemtic variable 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

Then I just have my functions definitions.

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 */

I really can't figure out why I get this error. Of course, I've searched around but the other people's error were due to function definition inside a header, which is not my case. Moreover, I do have include guards in my headers and they are different the one from the other.

I've also tried this: extern const address_s registerToEeprom[NB_OF_INIT_REGISTERS]; in i2cOptConfig, with the initialization of registerToEeprom in i2cOpt.h, but it doesn't work either.

Can someone help me?


Solution

  • You have a definition of the variable registerToEeprom in a header file. That means that any .c file that includes that header has a definition of that variable. When you then link your program after compiling each of the .c file, you get a multiple definition error.

    Moving the definition to a different header file doesn't help as you still have the same problem of a variable being defined in a header included by multiple source files.

    Put an extern declaration in your header file, then put the definition in exactly one source file, probably i2cOpt.c.