8

I downloaded the RTClib library from https://github.com/adafruit/RTClib. In every provided example, #include <Wire.h> is written immediately above #include "RTClib.h" at the top of the sketch.

Why is this necessary? I know the RTClib library requires the functionality of the Wire library but, if this is the case, why can't I just #include <Wire.h> at the top of RTClib.h? I have tried the latter, even using just the following sketch, but my sketch failed to compile.

RTClib.h:

// Code by JeeLabs http://news.jeelabs.org/code/
// Released to the public domain! Enjoy!

#ifndef _RTCLIB_H_
#define _RTCLIB_H_

#include <Wire.h>
...

#endif // _RTCLIB_H_

Sketch:

#include "RTClib.h"
void setup() {}
void loop() {}
Zilliput
  • 183
  • 1
  • 5

1 Answers1

4

Not getting too formal here, The arduino IDE looks at which libraries are included in the top level sketch when deciding what libraries to include in the compilation.

It would make sense for only RTClib.h to include Wire.h yes, but the IDE does not get the information to add Wire to the compilation list that way. It is a quirk of the IDE that makes implementing libraries that use other libraries a bit more painful.

BrettFolkins
  • 4,441
  • 1
  • 15
  • 26