Home > OS >  Arduino: Why same library is included multiple times
Arduino: Why same library is included multiple times

Time:07-09

I couldn't rationalize the reason that why (dependency) library is required when this very library has already been included in the required library itself. For example:

If I want to use SD.h, in the example code, SPI.h is required:

   #include <SD.h>  // SPI.h is already included in SD.h
   #include <SPI.h> // why include it here once more
   ...

however, if we go into the SD.h and related files, the SPI.h has already been included.

and this is not the only one, ILI9488.h, XPT2046_touchscreen.h, etc. all requires SPI.h when include them.

I have found someone on another topic said at arduino.cc that "if this is not included in the sketch, it (SPI.h) will not be compiled, besides, it is good to tell others that SPI.h is a dependency of the library you included" and I found the last part is especially unreasonable.

Maybe I understood him wrong, but it compiles, and they all worked great without including the SPI.h, of course, provided that I never used any of the methods from SPI.h.

However, I am sure I am missing something. I just don't know it and it haunting me ever since my code is working without including the SPI.h.

I appritiate if anyone can provide my some insight about this. Thanks in advance.

CodePudding user response:

Arduino doesn't have makefiles so the Arduino builder scans for #include directives to add the required libraries.

Old version of the build system required to list all libraries in the main ino file. That is why the examples list all the main header files of the libraries used.

  • Related