Few questions about including libraries and the resulting sketch size

Please bear with me, as I dont have a C or C++ background, and only know a bit about Java and C#.
(currently working toward a CS degree, but am only in my first year)

When you include a library in a sketch, what parts from the library are included into the sketch?
For example, a simple sketch such as the following yields a 1358 byte sketch size.:

#include <SPI.h>
#include <Ethernet.h>
void setup() {
}
void loop(){
}

What I cant tell, is what all is contained in the above sketch as far as code that caused the sketch to go from 466 bytes without the includes, to 1358 bytes with the includes.

Is the whole library (with all of its unused contents) being sucked into the sketch?
or
are just the used functions (and the required supporting code such as variables) sucked in?

I have a sketch that is currently at 23,886 bytes. While I am not out of room yet, there is still a fair amount of extra code I'd like to do, but am afraid I'll run out of room shortly.

When space starts to become tight, is it common for people to start re-writing libraries to trim out the unused code from within them to save space?

You can look at the source code to see what's in them, or just borrow the bits you need.

If you need more Flash space, or more typically more SRAM, you can move stuff into PROGMEM, use #define and const to move non-changing variables out of SRAM, or move up into a bigger chip, like Atmega1284P, with 16K SRAM (twice that of a Mega2560) and 128K Flash.
I offer a Duemilanove-style board with a'1284P as a kit or as assembled board, and with or without USB/Serial interface ($22 for kit less USB, $32 with USB (pictured), $15 to assemble, USPS small box flat rate shipping).


Cross Roads Electronics scroll down ~ 1/2 way for link to mighty1284 core files & pins_arduino.h for signal name mapping.

I like the thought of libraries, but am trying to understand more about what exactly gets pulled in when using a library.

I'm trying to figure out if i need to start manually reviewing the libraries and taking bits of unused code out of them, or if the compiler is already doing that for me. I cant help but wonder if I am not wasting (what will soon become) valuable storage space because I simply use the libraries as is.

The libraries I currently use are shown below:
<SendOnlySoftwareSerial.h>
<SPI.h>
<Ethernet.h>
<DS1302.h>
<DallasTemperature.h>
<OneWire.h>

For my current sketch, I've tried to avoid using SRAM as much as possible. I use F() to store what few character strings I have into flash. Using freemem, I get 1217 reported back to me, so I appear to have some room left for new variables in SRAM it appears. So it looks like I will possibly run out of room in Flash before I run out of SRAM.

When space starts to become tight, is it common for people to start re-writing libraries to trim out the unused code from within them to save space?

No. Unused code is not included in the resulting hex file. Only code that is actually called (under some circumstances that may never occur when the code actually runs) is included.

The sketch size jumped when you included those two files because they contain executable code.

Thanks, I think that answers my question -

It sounds like I wouldn't gain anything (other than a better understanding of the library) by manually editing the files down.

Now I'm off to find out more about shiftOut and quite possibly learn all I can about direct port manipulation - I find that the arduino shiftOut is painfully slow for a particular task I need to do. (I need to shift out 10 bits that covers a range of 0-4095 in a specified amount of time)

Can send it out as two bytes using SPI.transfer also, really fast.

I'm using a W5100 Ethernet shield at the moment- and as far as I am aware, I cant use the SPI pins because of this.

Is there a problem with SPI & W5100 ethernet? When not selected, the device is not supposed to be active on the 'bus' so that other devices can be selected for read/write access.

Is there a problem with SPI & W5100 ethernet?

NO. In fact the Ethernet shield contains a SD card, which is also an SPI device, so there are already two devices on the bus. Adding another is easy, as long as care is taken to manipulate all the CS pins and SPI parameters each time the new device is called upon. The SD library and the Ethernet libraries take care of this for existing devices.

Flash is one issue but SRAM usage is another as some libraries make harsh demands on it…
For Win, iOS, Linux, you can post-process the .elf created by a compile:

Ray

damage31:
When you include a library in a sketch, what parts from the library are included into the sketch?
For example, a simple sketch such as the following yields a 1358 byte sketch size.

Normally nothing, until you define dependencies to the library in your code (e.g. call functions in the lib). Note however that there might be global/static variables defined in header files which gets initialized at startup time, and there may be initialization code executed which inflates the sketch size. That's probably what happens in your case.

Cheers, Jarkko