0

Dear StackOverflowers,

I am learning to code object-oriented C++. For a project, I'm programming an esp32 using the Arduino core. I'm getting some linking errors that I really don't understand. There are many similar questions out there already, but they didn't help me to solve my question.

I have ISR functions that have to be declared globally. I've put those functions in a global.h and global.cpp file. The variables needed for those functions are declared in the .h file and defined in the .cpp file. To prevent things from being compiled twice I put this around it:

#ifndef GLOBAL_H
#define GLOBAL_H

#endif

The code compiles fine, but when the linking process starts I get an error message that says:

.pioenvs\nodemcu-32s\src\ULAM.cpp.o:(.bss.buttonState+0x0): first defined here
.pioenvs\nodemcu-32s\src\global.cpp.o:(.bss.oldButtonState+0x0): multiple definition of `oldButtonState'
.pioenvs\nodemcu-32s\src\ULAM.cpp.o:(.bss.oldButtonState+0x0): first defined here
.pioenvs\nodemcu-32s\src\global.cpp.o:(.bss.oldEncPos+0x0): multiple definition of `oldEncPos'
.pioenvs\nodemcu-32s\src\ULAM.cpp.o:(.bss.oldEncPos+0x0): first defined here
.pioenvs\nodemcu-32s\src\main.cpp.o:(.bss.oldButtonState+0x0): multiple definition of `oldButtonState'
.pioenvs\nodemcu-32s\src\ULAM.cpp.o:(.bss.oldButtonState+0x0): first defined here
.pioenvs\nodemcu-32s\src\main.cpp.o:(.bss.oldEncPos+0x0): multiple definition of `oldEncPos'
.pioenvs\nodemcu-32s\src\ULAM.cpp.o:(.bss.oldEncPos+0x0): first defined here
.pioenvs\nodemcu-32s\src\main.cpp.o:(.bss.buttonState+0x0): multiple definition of `buttonState'
.pioenvs\nodemcu-32s\src\ULAM.cpp.o:(.bss.buttonState+0x0): first defined here
.pioenvs\nodemcu-32s\src\main.cpp.o:(.bss.encoderPos+0x0): multiple definition of `encoderPos'
.pioenvs\nodemcu-32s\src\ULAM.cpp.o:(.bss.encoderPos+0x0): first defined here
.pioenvs\nodemcu-32s\src\main.cpp.o:(.bss.bFlag+0x0): multiple definition of `bFlag'
.pioenvs\nodemcu-32s\src\ULAM.cpp.o:(.bss.bFlag+0x0): first defined here
.pioenvs\nodemcu-32s\src\main.cpp.o:(.bss.aFlag+0x0): multiple definition of `aFlag'
.pioenvs\nodemcu-32s\src\ULAM.cpp.o:(.bss.aFlag+0x0): first defined here
.pioenvs\nodemcu-32s\src\main.cpp.o:(.data.mux+0x0): multiple definition of `mux'
.pioenvs\nodemcu-32s\src\ULAM.cpp.o:(.data.mux+0x0): first defined here

Some errors say that there are multiple definitions between ULAM.cppp and global.cpp, other say it's between ULAM.cpp and main.cpp. With the knowledge that I have about the compilation and linking processes it has to do something with the way I structured my #includes.

in main.cpp I have:
#include <Arduino.h>
#include "ULAM.h"

in ULAM.h I have:
#include <Arduino.h>
#include <Adafruit_MAX31856.h>
#include <SPI.h>
#include <PID_v1.h>
#include "../include/defines.h"
#include "global.h"
#include "ulamProgram.h"

in ULAM.cpp:
#include "ULAM.h"

in global.h:
#include <Arduino.h>
#include "../include/defines.h"

and in global.cpp:
#include "global.h"

Anybody knows what goes wrong here?

Daan van Driel
  • 165
  • 1
  • 2
  • 9
  • 1
    include guards do not prevent things being compiled twice if the file is included in multiple translation units –  Jul 12 '18 at 11:04
  • *"The variables needed for those functions are declared in the .h"*, you **declare** them `extern`, right? Else you probably **define** them (so with several inclusion, multiple definitions). – Jarod42 Jul 12 '18 at 11:05
  • If the variables are *only* used by these functions, you can have the variables just in the .cpp file. – Bo Persson Jul 12 '18 at 11:06
  • Header include guards only protects against multiple inclusion in a single [*translation unit*](https://en.wikipedia.org/wiki/Translation_unit_(programming)). – Some programmer dude Jul 12 '18 at 11:06
  • You don't show the variable declarations. – Jarod42 Jul 12 '18 at 11:13

0 Answers0