VStudio - multiple definition of `xxxxx'

Hi guy, I really need your help. I still get this error:
> Linking .pioenvs\esp12e\firmware.elf
> .pioenvs\esp12e\src\main.cpp.o:(.data.DebugX+0x0): multiple definition of `DebugX’
> .pioenvs\esp12e\src\customFunctions.cpp.o:(.data.DebugX+0x0): first defined here
> collect2.exe: error: ld returned 1 exit status
> *** [.pioenvs\esp12e\firmware.elf] Error 1

I use header guards in my file, so I do not understand why is this part of code redeclared. I had to miss some elementary in cpp programming but I still can’t get it to work.

Copy of my project:
https://bitbucket.org/fires/programingtesting/src/master/ESP8266/cpp/RedefinedError/

main.cpp
#include <Arduino.h>
#include “…/include/defines.h”
//#include “…/include/customFunctions.h”
// Time
uint32_t mTimeToSec = 0;
uint32_t mTimeSeconds = 0;
void setup() {
etc. etc. etc.

customFunction.h
#pragma once
#ifndef CUSTOMFUNCTIONS_H
#define CUSTOMFUNCTIONS_H
void connectWiFi();
void initializeOTA();
#endif

defines.h
#pragma once
#ifndef DEFINES_H
#define DEFINES_H
#include <jled.h>
#define BowlingLed 5
#define StatekLed 4
int DebugX = 1;
#endif

Thanks for the help, I read many articles and many issues but I still do not understand why DebugX is redefined if it is in header guard.

No definitions in header files. Also you have the classical include guards and then #pragma once in your header?

defines.h

#ifndef DEFINES_H
#define DEFINES_H

#include <jled.h>
#define BowlingLed 5
#define StatekLed 4
//declare the existence of this global variable.
extern int DebugX;

#endif

defines.cpp

#include "defines.h"

//define global var
int DebugX = 1;
2 Likes

Also see PlatformIO doesn't compile when header contains global variables

1 Like