First thing is if we are being pedantic, Arduino does not really use
libraries (ok the core code is a library) but all the others like LiquidCrystal, Wire, etc...
are not really libraries in the true sense. i.e. the code in the "library" is not compiled
into objects that are archived into an archive .a file that is linked against.
The IDE does not follow a traditional embedded build methodology.
It compiles the "library" modules to create .o files and then links in
the .o files rather than create a true library for it and linking against the library.
For this discussion, it doesn't really matter, since that is the way the IDE works.
I will definitely disagree with Nick about whether how code is structured
consumes more/less resources.
My Questions are, by moving code to an external lib:
- Will it affect performance?
- Will it use more memory in ROM (Compile time)?
- Will it use more memory in RAM?
- Will it work to have dependencies i many levels?
There are certain types of compiler and linker optimizations that can be done
depending on how the code is written, how the data is used & referenced, and how/where the source code lives.
Some of the optimizations can definitely affect performance, Flash, and RAM usage.
This can be particularly true for the AVR, which is 8 bit, Harvard Architecture, and
doesn't have the internal registers to handle pointers very well.
The compiler can be very smart when optimizing and the bigger the visibility the compiler
has, the deeper and better the optimizations can be.
For example if a variable is declared global and also static so that it is only known by
the compilation unit the compiler can do certain types of optimizations based on how
the code in that module uses it. When it is not static, the compiler can't do certain things
including eliminating data storage (RAM) for a variable that is never modified.
Other examples of things that are likely to be used when separating out functionality
into layers or libraries:
it takes less code and is faster to update a global variable vs a variable that
is a member of class object - even if they are same type.
using pointers are costly on AVR when updating data members.
foo.bar = 1;
will generate way less code than
foo->bar = 1;
particularly if foo is a global data structure.
If you use virtual functions in your classes for clean layering and expandability
you will pay the price in slight overhead in code size and performance vs having
direct function calls.
When making the code available to others, having your own Arduino "complications" installation.
When everything is part of the sketchbook directory, a single tar ball or zip image can be used to
give the user everything they need and there is no having to install the additional "libraries".
This is not to say that layering and using an Arduino "library" is a bad thing.
Clean layering and module separation/libraries often helps the code in the project be much more maintainable
and re-usable in other projects.
Clean layering and module separation can also make it easier to track your changes with
your source control tool. (Surely you are using one like git, svn etc...?)
So you can easily see and track changes since the changes will be specific the modules
within the library vs having a single large file or grouping of files.
Having your code in an Arduino "library" can allow you to have multiple sketches
that use it which is often very useful for testing.
For the most part, I'd say if cycles and ram space are not super critical go
down a more organized path, which is likely to be a cleaner separation
into multiple modules and and potentially create an Arduino "library".
I say potentially since there is not much difference between a good module
separation and an Arduino "library" as far as a single sketch is concerned.
The benefit of an Arduino "library" is if you want to share the "library" code
across multiple sketches.
The best way to get a handle on code impact it is to starting looking at the
code generated by the compiler.
If you are on a *nix machine I posted a patch a while back for how to automatically create listing files
every time you build the code with the 1.5x IDE.
Sorry if you are using Windows, while it can also be done on Windows it is not as easy.