basic compiling improvements

  1. Order of File Combining: I noticed that just before compiling, Arduino adds the supporting user code files AFTER the one which contains the main loop. The only reason this is done in c projects is to reduce clutter at the beginning of main.c. Putting them BEFORE the main loop in the Arduino IDE would have the advantage of not needing to create and include header files for each supporting file and shouldn't have any effect on existing code.

  2. Compiler Optimization: An IDE will generally optimize out functions which aren't used in the main user code. Ardiuno evidently does this in the main user code (where it makes little difference) but fails to do so in the supporting libraries. This means that you end up with massive amounts of space taken up with un-used functions unless you want to manually edit every every library you include (and continue to edit them as you update your own code). I'm guessing that this is so libraries don't have to be pre-compiled every time you change the user code. However, I've worked with 3 different IDEs (CCS, IAR, Keil uVision) which somehow manage to edit out un-used functions from libraries without having to recompile everything, so there should be a way to do this. As it is, the lack of this feature pretty much negates any other code size optimization that is performed.

Dustin Soodak

p.s. It would also be nice to have "navigate forewards/backwards" and "go to definition", though the latter would probably be a lot of work to implement.

  1. Compiler Optimization: An IDE will generally optimize out functions which aren't used ... fails to do so in the supporting libraries.

You are going to have to provide evidence. Your claim directly contradicts everything reported on this forum.

Last time I looked I found a post saying that unused functions are removed from main code but not from libraries. This search was prompted by a suggestion in a project to manually remove unused functions from supporting cpp files.

However, after your response I did another quick search and found that this was actually fixed years ago:

http://playground.arduino.cc/Code/LibPatch0007

So I tested by compiling for Uno and got:
466 bytes with an empty project
1710 bytes when #include "Wire.h" was added (but still no code except for empty setup() and loop())
few 10s or 100s of bytes for each new function added.

So it looks like the current version DOES get rid of unused functions, but there is still SOMETHING sneaking in.

Maybe it doesn't remove unused variables and data?

So it looks like the current version DOES get rid of unused functions, but there is still SOMETHING sneaking in.

At the bottom of Wire.h:

extern TwoWire Wire;

An instance of the class is being created. The extern keyword makes the instance available to other compilation units, so the linker can't figure out that you don't actually need it. The assumption, quite realistically, is that if you didn't need the instance, you wouldn't have included the header file.