With gcc you can get the unused functions to be stripped off if you compile the object files with the -fdata-sections -ffunction-sections options. Then when you go to make your final executable you link it with -Wl,--gc-sections
As an example suppose utilities.c contains the functions above from anduril's example, utilities.h is a header file that declares those function prototypes and main.c contains the following code which references only one of the three library functions:
Code:
#include "utilities.h"
int main()
{
one_b();
}
Compile utilities.o as follows. This puts one_a, one_b and one_c into utilities.o but marks them in such a way that they can later be removed by the linker on a per-function basis
Code:
gcc -fdata-sections -ffunction-sections -o utilities.o -c utilities.c
Then compile main as follows
Code:
gcc -o main main.c utilities.o -Wl,--gc-sections
If you list the contents you will see only one_b is included
Code:
$ nm -Cg main
...
0000000000400470 T main
000000000040056d T one_b
...
For some MinGW versions this will not work when you produce windows executables. To get it to work though you can patch the linker with the instructions here: https://sourceware.org/bugzilla/show_bug.cgi?id=11539