Pages: [1]   Go Down
Author Topic: Compiling libraries  (Read 305 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As a new boy to Arduino Uno's can some one please tell me why my code is so big when it compiles. Something like 350 lines of actual code (ignoring the //remarks) comes out at 29K of code acording to the Ardunio IDE whe it compiles. OK so I've got 6 #include statements - SPI.h, Ethernet.h, EthernetUdp.h, Time.h, SD.h, and EEPROM.h.

Now for instance I'm not using Minutes(x) from the Time library so will that part of the libray code be include in my sketch or not?
Likewise I'm not using DCHP or IPaddress for the webserver bit as all the IP addresses are hard coded but looking at Ethernet.h it eventually includes DCHP.h and IPaddress.h so do those two bits get compiled as well even though there not directly called for? Interestingly if I do use DCHP the compiler tells me that the sketch size is 33K and the compile failed.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now for instance I'm not using Minutes(x) from the Time library so will that part of the libray code be include in my sketch or not?
Do you know that no method you are using uses Minutes()? If Minutes() is a function, and is never called, it will not be in the hex file produced. If Minutes() is a method, and the class containing it is instanced, then it will be included.

Quote
so do those two bits get compiled as well even though there not directly called for?
Yes, they get compiled. No, they do not get linked in. The reported size comes from the linker, not the compiler.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From what your saying: As nothing I've coded uses Minutes(x) then it does not get included in the machine code. Unless that is that function is called within Time.h or one of the other included libraries.

Iv'e started to build a tree of the library calls for the whole project and notice that the libraies thmeselves call things like Arduino.h and avr/pgmspace.h several times over. Do they get added in just once or take up more space for every call? Does the compiler/Linker say to itsself we already have these methods and functions just use them again?

I'm not sure that I understand the comment about the linker. Are you saying that unused library functions and methods take space in the HEX file or not? If they do how do I cut down the total size of the sketch without reverse engineering the libraries, I need to make room for the functionality that I have not coded yet?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The first thing that you need to be aware of is the distinction between a function and a method. A method is a function that belongs to a class.

If you use a class, all its methods are part of the hex file, used or not.

If you don't use a function, it is not part of the hex file.

Quote
Do they get added in just once
If you look at the header files, you'll see multiple inclusion guards that prevent code from being compiled in more than once.

Quote
Does the compiler/Linker say to itsself we already have these methods and functions just use them again?
Something like that. Although, the linker isn't really talking to itself.   smiley

Quote
Are you saying that unused library functions and methods take space in the HEX file or not?
See the first paragraph...

Quote
If they do how do I cut down the total size of the sketch without reverse engineering the libraries, I need to make room for the functionality that I have not coded yet?
The answer is that you can't.
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6381
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

IIRC, the ethernet code is not very good about excluding unused functionality.  You may end up with DHCP and DNS code whether you're using them or not.

In general, you can connect to your temp directory after a build (use "verbose" compilation to figure out where that is) and use the "avr-nm" tool to look at the .elf file that is there.  The useful form of the command  looks like:
/path/morepath/hardware/tools/avr/bin/avr-nm --numeric-sort --demangle --print-size *.elf | grep -i " t "
The output from "blink" looks like this:
00000068 0000000a T port_to_mode_PGM
00000072 0000000a T port_to_output_PGM
0000007c 0000000a T port_to_input_PGM
00000086 00000014 T digital_pin_to_port_PGM
0000009a 00000014 T digital_pin_to_bit_mask_PGM
000000ae 00000014 T digital_pin_to_timer_PGM
000000c2 T __ctors_end
000000c2 T __ctors_start
000000c2 T __dtors_end
000000c2 T __dtors_start
000000c2 T __trampolines_end
000000c2 T __trampolines_start
000000ce T __do_copy_data
000000da t .do_copy_data_loop
000000de t .do_copy_data_start
000000e4 T __do_clear_bss
000000ec t .do_clear_bss_loop
000000ee t .do_clear_bss_start
000000fc T __bad_interrupt
00000100 0000002e T loop
0000012e 0000000c T setup
0000013a 00000090 T __vector_16
000001ca 000000b2 T delay
0000027c 00000076 T init
000002f2 0000007e T pinMode
00000370 000000a8 T digitalWrite
00000418 0000001e T main
00000436 T _exit
00000438 t __stop_program
0000043a T _etext

You don't need to know what everything IS to look through and see if there are big functions that you think you aren't using...  (However, fixing the ethernet library to not include DHCP (if that's necessary) involves making a copy of the library and figuring it out enough to edit away the pieces you don't want.  Perhaps someone will have already done it.  The libraries you're using (ethernet and SD) are not "tiny", even when using only minimum functionality.)

Logged

Pages: [1]   Go Up
Jump to: