Go Down

Topic: Do unused functions in a library use memory? (Read 926 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


Go Up