Arduino IDE 2.0.4 Including Header Multiple Times

I have some code that in 5 different files I include a .h file . The file contains:

#ifndef RemoteStatus_H
#define RemoteStatus_H
#include "ArduinoTimer.h" 

ArduinoTimer ledONtime;
ArduinoTimer ledOFFtime; 

This file is included 5 times and I get the following errors:

/Users/Kurt/Library/Arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLEHandler.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:7: multiple definition of `ledOFFtime'; /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLECamera.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:7: first defined here
/Users/Kurt/Library/Arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLEHandler.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:6: multiple definition of `ledONtime'; /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLECamera.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:6: first defined here
/Users/Kurt/Library/Arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/InputHandler.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:7: multiple definition of `ledOFFtime'; /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLECamera.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:7: first defined here
/Users/Kurt/Library/Arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/InputHandler.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:6: multiple definition of `ledONtime'; /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLECamera.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:6: first defined here
/Users/Kurt/Library/Arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/OAG.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:7: multiple definition of `ledOFFtime'; /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLECamera.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:7: first defined here
/Users/Kurt/Library/Arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/OAG.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:6: multiple definition of `ledONtime'; /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLECamera.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:6: first defined here
/Users/Kurt/Library/Arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/RemoteStatus.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:7: multiple definition of `ledOFFtime'; /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLECamera.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:7: first defined here
/Users/Kurt/Library/Arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/RemoteStatus.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:6: multiple definition of `ledONtime'; /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLECamera.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:6: first defined here
/Users/Kurt/Library/Arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/Sony_OAG_Focuser_RAYTAC.ino.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:7: multiple definition of `ledOFFtime'; /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLECamera.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:7: first defined here
/Users/Kurt/Library/Arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/Sony_OAG_Focuser_RAYTAC.ino.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:6: multiple definition of `ledONtime'; /private/var/folders/2b/fgk4_0b13bl41w3jww7ndhpw0000gp/T/arduino/sketches/9926221EECF16418237CC5ACB14367E2/sketch/BLECamera.cpp.o:/Volumes/Astrophotography/Arduino 2022/Sony_OAG_Focuser_RAYTAC/RemoteStatus.h:6: first defined here
collect2: error: ld returned 1 exit status

Why is this happening to me?

Kurt

It is because each source file you include this into will be defining those two objects.

That is you will multiple ones. Not sure if you want multiple or not.

If you want multiple you could probably get away with changing those two defines:
into:

static ArduinoTimer ledONtime;
static ArduinoTimer ledOFFtime; 

If you really only want one of these objects, then should change these to:

extern ArduinoTimer ledONtime;
extern ArduinoTimer ledOFFtime; 

And in one of your sources files you would then delare the objects like:

ArduinoTimer ledONtime;
ArduinoTimer ledOFFtime;

Hope that makes sense.

Kurt

Thanks. I should have been more clear but my question is really since I have the lines

#ifndef RemoteStatus_H
#define RemoteStatus_H

Then I expected that the include file contents would only be included once. It's like the compiler doesn't see the RemoteStatus_H exists.

I have seen other strange things like this. I expect that a definition that occurs in the .ino file would have global scope. If I have a file main.ino in folder main doesn't the IDE include this file first and all other files after it? So if I define say a variable of global scope in main.ino all the other files in the sketch should see this declaration.

Kurt

It is always difficult to know exactly what is going on without seeing your sources. and like. You say that you have 5 source files, but have not specified what type (.c .cpp .ino)? My assumption is maybe one INO and 4 c or c++ or combination.

But specifically more than one thing compiled.
The #ifndef stuff is great to only have the contents of that header file be processed once in each thing compiled. This is very helpful when for example a header file includes another header file So again if the INO and other .cpp files each include this file they will declare those objects each time.

Note: my first response showed one way to do it. I have also done it another way.
Something like:

#ifndef RemoteStatus_H
#define RemoteStatus_H
#include "ArduinoTimer.h" 

#ifdef DEFINE_STATUS_OBJECTS
ArduinoTimer ledONtime;
ArduinoTimer ledOFFtime; 
#else
extern ArduinoTimer ledONtime;
extern ArduinoTimer ledOFFtime; 
#endif
...
#endif // RemoteStatus_H

And then in one of the files before you include this file add in the define

#define DEFINE_STATUS_OBJECTS
#include "RemoteStatus.h

Edit: the reason I mentioned about different file types. If all 5 files are .ino files, Arduino will combine all 5 of them into one thing that is compiled.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.