Am I duplicating header files?

I am building a project using an Arduino Uno, an Adafruit 2.8” capacitive touchscreen and a DS3231 Real Time Clock.

To start my sketch off, I copied the header files from the various test sketches into one combined header section. I deleted obvious duplicates. I am now well into my sketch and everything works. However, I think I may have duplicate header files in some cases – the same header file seems to be defined differently. I assume that, if you define a header file twice, you are using twice as much memory?

In particular I seem to have too much with regards to the Adafruit_ILI9341.h
There is -

type or paste code here

#define TFT_DC 9
#define TFT_CS 10

type or paste code here

and

type or paste code here

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

type or paste code here

Here is what I have -

type or paste code here

//This program monitors the run time of a water pump and shuts it down if it runs too long
#include "SPI.h" // this is needed for display
#include "Adafruit_GFX.h" // Core graphics library
#include "Adafruit_ILI9341.h"

// The display also uses hardware SPI, plus #9 & #10
#define TFT_DC 9
#define TFT_CS 10

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

#include <Wire.h> // this is needed for FT6206
#include <Adafruit_FT6206.h>

// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS3231 rtc;
// DS3231 device address
#define deviceAddress 0b1101000 //0x68

type or paste code here

Any help would be much appreciated, please.

My noobie efforts to use code tags screwed up. I am trying again below.

I am building a project using an Arduino Uno, an Adafruit 2.8” capacitive touchscreen and a DS3231 Real Time Clock.

To start my sketch off, I copied the header files from the various test sketches into one combined header section. I deleted obvious duplicates. I am now well into my sketch and everything works. However, I think I may have duplicate header files in some cases – the same header file seems to be defined differently. I assume that, if you define a header file twice, you are using twice as much memory?

In particular I seem to have too much with regards to the Adafruit_ILI9341.h
There is -

#define TFT_DC 9
#define TFT_CS 10

and

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

Here is what I have -

//This program monitors the run time of a water pump and shuts it down if it runs too long
#include "SPI.h"           // this is needed for display
#include "Adafruit_GFX.h"  // Core graphics library
#include "Adafruit_ILI9341.h"

// The display also uses hardware SPI, plus #9 & #10
#define TFT_DC 9
#define TFT_CS 10

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

#include <Wire.h>  // this is needed for FT6206
#include <Adafruit_FT6206.h>

// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS3231 rtc;
// DS3231 device address
#define deviceAddress 0b1101000  //0x68

Any help would be much appreciated, please.

Thanks for fixing your boo-boo. !

just FYI, you could have edited the original post to rectify the error, but you made your best effort as a newbie !

Thanks

1 Like

What do you mean by "too much"? What problem are you trying to solve?

Perfectly normal although modern thinking says to use const rather than #define, even Arduino mentions it in the documentation.
The defines tell the pre-compiler to replace all occurrences of the characters TFT_DC with 9, and TFT_CS with 10.

No. If you try and use the same name twice (for variables, or classes/structs, etc) whether or not for the exact same thing, the compiler will complain.

It's not unusual for two or more things you're using to have some common header, so a properly defined header file has guards of some kind so that the header content only "takes effect" once.

If you took two similar but actually different headers and combine them, you would have to have deleted all the duplicates to compile successfully. Then you might be using more memory, but not double. You can continue to delete stuff: if the sketch still compiles, then nothing was actually using it.

Thanks - now I see the edit icon!

Thanks Kenb4!

Thanks sonofcy.
I must look into using const instead of #define - I was unaware of this.

creating a single header file contianing all possibly header files isn't that uncommon, but can become problematic when trying to reuse code

knowing what should and shouldn't be in a header file can avoid confusion

header files should NOT define variables. variable definitions allocate memory and should be done in .ino/.cpp files. Header files often declare variables and functions, making their existence known, variable type, hence size, # of function arguments and return type.

header files have guards that skip the contents of a header file that already been included in a file

header files should include declarations of variable/functions intended to be accesses extern to the file it is defined in. Some variable/funcitons may be local to that file and should not be declared in the header file

defines/constant that need to be known when using variable/functions declared in the header file should be made in the header file and be made know to the .ino/.cpp file with those variable/functions by including the header file in the corresponding .ino/.cpp file which will also insure the definitions and declarations match

Header files should NOT define variables.
At least, your phrasing would make more sense if that's what you meant.

1 Like

Per responses from @gcjr and others, properly written header files do not consume memory. If you look behind the scenes at the Arduino build process you'll see that dozens (maybe hundreds) of .h files get #include(d) for even the simplest sketch containing just empty setup() and loop() functions. (program) Memory is not consumed unless functions declared in those headers are invoked. (data / RAM) Memory is not consumed unless data structures (i.e. variables, class instances, etc) declared in those headers are instantiated. That's even before optimizations by the compiler and linker take place. For a basic guide to breaking large code into multiple files, see My Post #5 in this Thread.

Think of header files as your code "menu". Very good for seeing what things are being offered to your project. You could pass around a bunch of menus to other guests. The menu is not the actual "food". The .cpp file has the "food" in it.

-jim lee

1 Like

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