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:
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!
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!
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?
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.