Go Down

Topic: Do unused functions in a library use memory? (Read 965 times) previous topic - next topic

yerg2k

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  ;)

Triffid Hunter

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.

kg4wsv

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

-j


westfw

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.

kg4wsv

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


Triffid Hunter

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

ChrisCooper

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?

mellis

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.

jcgalvezv

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.

Go Up