Do I have to include unneeded libraries in the sketch?

Status
Not open for further replies.

monkeybiscuits

Active member
Say I have a library with two parts (all in the same folder):
1) foo.cpp / foo.h which #include “Bounce.h”
2) bar.cpp / bar.h which #include “Encoder.h”

I want to write fooExample.ino which should only require “Bounce.h” so I
#include <Bounce.h>
#include “foo.h”

How can I get that sketch to compile without #include <Encoder.h> too? fooExample.ino doesn't need it! I'm wondering if this is just one of those “Arduino includes are a pain. Just deal with it” things. I'd love to hear any solution that doesn't require me to include unneeded libraries in the sketch.

If it sounds like a problem with the way I'm coding it, the library can be found here.
https://github.com/monkeybiscuits/MIDIcontroller
 
to put it simple: every used library in other libraries needs to be included in the main sketch, for example: the ili9163c library needs the spi library included in the main sketch as well (otherwise you'll get a similar error), even though the spi functions are not in the main sketch, but in the ili9163c library itself, it's just the way arduino does it sadly..
 
to put it simple: every used library in other libraries needs to be included in the main sketch

Would you believe Arduino 1.6.6 has finally changed this? It attempts to discover which libraries are needed, without requiring them to all be included in the sketch. I tested with Ethernet.h and Audio.h. It seems to work, but I haven't tried more complex cases. This is a huge amount of new code in Arduino's build system, so it's likely to have subtle issues. I'm sure over time as Arduino releases future versions, bugs will get fixed. But it does generally work.

Of course, if you want to publish sketches that are compatible with 1.6.5 or earlier, you do need to have an #include in your sketch for every library, even if you don't use those libraries directly.
 
Yes, the linker omits unreferenced functions and variables.

There is a subtle distinction between "unreferenced" and "unused". If something references a function, such as passing a pointer to that function to some other code, the linker can't know whether that other code ever actually uses the pointer to call the function, unless it can deduce that pointer isn't ever referenced by other code.

Likewise for data, suppose you have arrays of font data and a conditional test to use one of many fonts. Unless the inputs to the conditional test are constants known at compile time, the linker can't know in advance which conditions will be true, so all the font data would be considered referenced, even if you only use 1 at runtime. The mfGFX library does this, which was one of the main reasons I didn't use it and wrote a totally different way to define fonts for ILI9431_t3.
 
Status
Not open for further replies.
Back
Top