0

I am working on a project on Arduino ESP32 and I have a lot of Global variables (for data generation). I have decided to create a library in order to orgenise my work a little better. But I use this library into other librari's that I had to create for other usage. after compilation it I have the following error :

sketch\OX2inj_LEVEL_OX2.cpp.o:(.data.addrChipId+0x0): multiple definition of `addrChipId'
sketch\First_Useage.cpp.o:(.data.addrChipId+0x0): first defined here
sketch\OX2inj_LEVEL_OX2.cpp.o:(.bss.ChipID+0x0): multiple definition of `ChipID'
sketch\First_Useage.cpp.o:(.bss.ChipID+0x0): first defined here

here is my .ino (main) code :

#include <Arduino.h>
#include "Var_Str_EEPROM.h"
#include "Def_Global_Var.h"
#include "First_Useage.h"

//---------somthing

void setup() 
{
  Serial.begin(115200);
  //---------somthing
  Serial.println(ChipID.ReadStrEEPROM());
  //---------somthing

}

void loop() 
{
  //---------somthing
}

here is my "Def_Global_Var.h" code

#ifndef Def_Global_Var_H
#define Def_Global_Var_H

#include "Var_Str_EEPROM.h"


uint16_t addrChipId = 1;

VarStrEEPROM ChipID(addrChipId);

#endif

here is my "First_Useage.h" code

 #ifndef First_Usage_H
#define First_Usage_H

void getchipid();

#endif

here is my "First_Useage.cpp" code :

#include "First_Useage.h"
#include <Arduino.h>

#include "Var_Str_EEPROM.h"
#include "Def_Global_Var.h"

void getchipid()
{
  uint32_t chipId = 0;
  for(int i=0; i<17; i=i+8) 
    chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
  ChipID.WriteStrEEPROM(String(chipId));
}

My understanding is that, when I use the #include "Def_Global_Var.h", the programme thinks that : "I am calling the library" and it sees that it has been called before and it does not like it. Is it somehow correct ? and if it is(or not) correct what should I do?

EDIT : sorry I have put the wrong part of the prog. it has been corrected now

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Parham
  • 1
  • 1

1 Answers1

0

The actual cause is that the header is included into several source files, so you end up with multiple conflicting definitions of these variables in your .o files.

You shouldn't normally define global variables in header files at all; you should only declare them as extern:

#ifndef Def_Global_Var_H
#define Def_Global_Var_H
...
extern uint16_t addrChipId;
...
#endif

The second step is to define the variable in the corresponding .cpp file, this time without the extern keyword:

// Def_Global_Var.cpp

uint16_t addrChipId = 1;

Since Def_Global_Var.o gets linked only once, there should be no more conflicts.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • OR since this is C++, and it doesn't look like `addrChipID` is actually modified, OP could declare it `constexpr`. – Spencer Jun 02 '22 at 15:22
  • So you are saing that I can not use a "global variable Library". thank you . I will find another solution in that case. @Spencer : the problem is that I need to take information from libraries that I have created and in these libraries I have to be able to change these information too. – Parham Jun 03 '22 at 13:33
  • I'm not saying that at all. You can put global variables into a library, you just have to write it correctly to avoid duplicates. – Thomas Jun 03 '22 at 20:24