Unused libraries end up in the final .hex file?


I've been making some libraries and I noticed that if I include a library in a sketch but don't reference the library in any way, the sketch still builds with the library code included in the .hex file.

On this page:


It says

These .o files are then linked together into a static library and the main sketch file is linked against this library. Only the parts of the library needed for your sketch are included in the final .hex file, reducing the size of most sketches.

However, at least for my custom libraries, this seems not to be the case. Am I doing something wrong?

  • Ben

You're not doing anything wrong. The quote from the build process page only applies to the core Arduino files (in hardware/cores/arduino) not to libraries in hardware/libraries. From http://www.arduino.cc/en/Reference/Libraries:

Because libraries are uploaded to the board with your sketch, they increase the amount of space it takes up. If a sketch no longer needs a library, simply delete its #include statements from the top of your code.

I'm hoping to change this so that libraries are treated the same as the core files, but I haven't had a chance to yet. Help welcome!

maybe apply a recursive make to contributed libraries, and leave it to the contributor to correctly build libraries from the makefile?