2

I really need your help. I use VisualStudio and PlatformIo 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.

1
  • In the file defines.h, you declare an integer DebugX. It is not a good idea to declare an integer in a *.h file. The compiler somehow sees two declarations of DebugX. There are no strange characters and there is no typo error. It works in the arduino ide without the pragma. Perhaps the pragma works against the header guard for that specific ide.
    – Jot
    Commented Mar 24, 2019 at 17:34

1 Answer 1

4

Included files are included before compilation, really replacing the #include with the content of the file.

If you write int DebugX = 1; you define a variable. And it is defined in every source file which includes it. Then the linker finds it in more then one unit and ends with "multiple definitions" error.

The variable must be defined only in one compilation unit (.c, .cpp). For example in your case customFunctions.cpp. To make it available in other source files put extern int DebugX; into the .h file.

Not the answer you're looking for? Browse other questions tagged or ask your own question.