#include usage question

Is it legal in the Arduino implementaton to have includes within included files? My target CPU is the Teensy 3.2 and I'm trying to use the Adafruit_SSD1306 display. I know the library works with my hardware because the example program will run just fine.

For clarity, I've put all the code for the display in "display.cpp" and "display.h" and display.h is #included in my main .ino file.

In the .h file there is a declaration "Adafruit_SSD1306 display;" and in the .cpp file this object is referenced by use of extern:

#include <Adafruit_SSD1306.h>
extern Adafruit_SSD1306 display;

The intention was to put all display-related code in display.cpp which is #included in my main .ino, and which in turn would make calls to the various functions in the Adafruit library. This way if I wanted to change to a different display, all I'd need to do is rewrite the display section. Makes sense to me at least!

However the compiler flags every call to the Adafruit_SSD1306 library as an "undefined reference", as if there is something illegal about this syntax, but I haven't been able to figure out why. Since the Adafruit_SSD1306 library is in my sketchbook library folder the compiler shouldn't have any trouble finding it. Unless I'm missing some error in my syntax, I have to wonder if Arduino IDE allows you to have #included files in files that are themselves #included?

Thanks for any guidance on this and happy new year!

Bob

However the compiler flags every call to the Adafruit_SSD1306 library as an "undefined reference"

How are you calling the library ?

Having a peek at your code that produces the error might reduce the guesswork somewhat.

Yours,
TonyWilk

w9ran:
In the .h file there is a declaration "Adafruit_SSD1306 display;" and in the .cpp file this object is referenced by use of extern:

#include <Adafruit_SSD1306.h>
extern Adafruit_SSD1306 display;

you are creating an instance in the header file but it looks like you haven't told the linker.

have you tried adding this:

extern Adafruit_SSD1306 display;

to your ino file?

w9ran:
Is it legal in the Arduino implementaton to have includes within included files?

Yes. Almost every library does it.

Actually I got it fixed with the help of a software engineer friend. The problem was, the declaration was in the header file and the extern reference was in the .cpp file. He informed me there were good reasons for always putting the declaration in the .cpp file, and when I swapped them, it compiled. So evidently at least in the Arduino version, that's not just a good idea, but is a requirement.

Glad to know that included files can include others. Thanks for the replies, learning continues!

Bob

The only thing you have to be aware of is that if you include other files, you can end up having an #include try to include a file that's already been included - still with me? :slight_smile:
To avoid this, it is best to add a 'guard' in each include file that looks like this:
#ifndef NAME_OF_INCLUDE_FILE_H
#define NAME_OF_INCLUDE_FILE_H
//
// The actual include file goes here
//
//
#endif

You'll see this sort of thing in most of the standard library .h files.

Pete