Skip to contentSkip to conversation

Way to leave-out unused functions while linking to a library

DISCUSSION Shriramana Sharma 1 participant 1 message May 23, 2006

lore
Share & tools
lore
#0 lore raw

I asked this question on this list some time back, IIRC -- how to leave-out unused functions while linking to a library?

The normal behaviour is that if a library.c contains two functions fna() and fnb() and main.c calls only fna(), compiling library.c and main.c and linking to form an executable will result in the fnb() function getting included in the final executable, though it is never used in the entire course of the program.

So how to stop fnb() from getting linked in?

Over at the GNU GCC list, a good soul gave us the solution. Use -

-ffunction-sections -fdata-sections

as compiler options and

--gc-sections

as a linker option. If you are calling the linker indirectly via the compiler gcc (i.e. if you are directly creating executables using gcc without a separate call to ld) then give it the options:

-ffunction-sections -fdata-sections -Wl,--gc-sections

[exactly as it is, with the comma] so that the compiler passes the appropriate option on to the linker. This method worked for me!

Signature · 3 lines
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.1
$ date [] CCE +2006-05-23 W21-2 UTC+0530