Pages: [1]   Go Down
Author Topic: Do unused functions in a library use memory?  (Read 663 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
everywhen
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello. I am working on a library for cheap RF links which need encoding to work. These typically come in pairs of two devices, a transmitter, and receiver.

In the event that a sketch is only using the transmit routines, and the receive code is not used, will having the receive code in the library consume sketch space?

I am trying to determine whether I should make one library for both, or two separate libraries. If a sketch is not using the receive code, I would like it not to take up room in the sketch.

P.S. This forum's search engine doesn't work very well  smiley-wink
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure if sketch uses gcc, but if it does, gcc and ld have options to leave out functions that are never used. You want to either make separate object files for each section you want to be optional, or find the gcc option that puts each function in a separate section. Then, you want to find ld's section garbage collection option which removes unreferenced sections.

I believe avr-libc does this already, or even simple programs would be enormous.
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If it's a true library (in gcc terminology, i.e. libsomething.a), only the functions actually used are included in the executable.

-j

Logged

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

Quote
If it's a true library (in gcc terminology, i.e. libsomething.a), only the functions actually used are included in the executable.
Building such a "true" library generally means putting each function that might not be needed in a separate source file, I think.
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Building such a "true" library generally means putting each function that might not be needed in a separate source file, I think.
I don't think so.  Building a .a file requires use of tools you don't normally encounter in typical compilation, though.  ar for sure, to create the library, and possibly ranlib depending on your platform.  Then if you go with a shared library there are still more steps to get a .so from the .a, but that's beyond the scope of avr-gcc/atmel/arduino.

If you don't have a .a file, you don't have a library.

Now, if you do have every function in a separate .o file, and only link the .o files you need, then you are pretty much manually handling what the linker will do automatically if you have a library.

-j

Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I haven't played with .a/.la much, but .so is easy; just gcc -o libsomething.so --shared source1.o source2.o source3.o
Logged

London, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks - As do I! :)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have no prior history with C and C++ so this is a steep learning curve for me.

My question is a follow on from the above:

For example I have written a Wii Nunchuck librbary with separate methods for the joystick code and the accelerometer code, the joystick code is also split into several parts, one treating the joystick as an analog device and two others treating the joystick as a 4-way digital or 8-way digital input.

Ideally if I write an app that uses only the 8-way digital joystick methods I don't want the linker to include all the other code from the library, and it seems to be overkill to separate the code out into several separate code bases for inclusion.

Does anyone know what command line I would have to use to compile .a instead of .o files for my libraries, so that the linker can be a little cleverer?
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you set build.verbose to true in your arduino preferences file you can see the command lines it uses to build your sketch, which should help you get started.
Logged

El Salvador, Central America
Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As far as I know, all the functions included in a compiled source code are included in the linked file. i.e: you have a file named abc.c which has inside functions a(), b(), and c(). Even if you only use a() the funtions b() and c() are also included in the linked program.
Logged

Pages: [1]   Go Up
Jump to: