0

I stumbled upon the following unexpected behaviour and am curious about its cause.

header.h:

#include <Time.h>
[...]

main.ino:

#include "header.h"
#include <Time.h>
[...]

My code is working correctly with this setup. However, if I comment out the Time.h library in my main.ino, my code is working not correctly anymore:

#include "header.h"
//#include <Time.h>
[...]

I can still access the functions of Time.h, but they are returning garbage time.

Why do I have to include the library in my *.ino, although I am already including it in my header file? Is there a best practice, on where to include libraries? Shall I simply always include all libraries in my *.h and *.ino file?

  • you want time.h or TimeLib.h? Time.h is ambiguous – Juraj Jun 25 at 9:32
1

Firstly you should be including TimeLib.h not Time.h - the library header name has been changed to avoid conflicts on case-insensitive systems (Windows) and the internal time.h header in many compilers.

Secondly you should always include all your libraries in your INO file directly. This is because the IDE parses the INO file before compiling looking for any included libraries. It then adds these found libraries to the list of libraries that need to be compiled. Without this you won't get the source code for your libraries compiled properly.

  • Yes! This (Secondly) also often applies to #include <Wire.h>, if another library uses I2C. – DataFiddler Jun 25 at 10:47
  • 1
    @DataFiddler And SPI, etc. – Majenko Jun 25 at 10:49
  • I don't include includes of libraries. the Arduino builder can handle it. I use Ethernet.h without SPI.h in sketch, UnoWiFiDevEdSerial1.h without Wire.h in sketch, ... – Juraj Jun 25 at 11:09
  • @Juraj As long as you're using a version of the IDE that uses Arduino Builder - or UECIDE of course which does it natively. For everyone else in the world (like those fools that apt-get install arduino, or idiots that use plugins to eclipse or VS ;) ) it won't work. So best to include them anyway since there's no down side, only up sides. Of course, if no one else uses your code that's fine... – Majenko Jun 25 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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