Is there any way to tell the compiler (gcc/mingw32) when building an object file (lib*.o
) to only expose certain functions from the .c file?
The reason I want to do this is that I am statically linking to a 100,000+ line library (SQLite), but am only using a select few of the functions it offers. I am hoping that if I can tell the compiler to only expose those functions, it will optimize out all the code of the functions that are never needed for those few I selected, thus dratically decreasing the size of the library.

- 20,590
- 28
- 126
- 201
2 Answers
I found several possible solutions:
This is what I asked about. It is the gcc equivalent of Windows' dllexpoort
:
- http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Code-Gen-Options.html (
-fvisibility
) - http://gcc.gnu.org/wiki/Visibility
I also discovered link-time code-generation. This allows the linker to see what parts of the code are actually used and get rid of the rest. Using this together with strip
and -fwhole-program
has given me drastically better results.
- http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Optimize-Options.html (see
-flto
and-fwhole-program
)
Note: This flag only makes sense if you are not compiling the whole program in one call to gcc, which is what I was doing (making a sqlite.o file and then statically linking it in).
The third option which I found but have not yet looked into is mentioned here:
That's probably the linkers job, not the compilers. When linking that as a program (.exe), the linker will take care of only importing the relevant symbols, and when linking a DLL, the __dllexport mechanism is probably what you are looking for, or some flags of ld can help you (man ld).

- 35,651
- 4
- 70
- 100
-
I am using `.so` files, not DLLs. And I think once the source is compiled, the linker can't know which parts are needed by which, and must import the whole thing. – Baruch Jul 17 '11 at 04:20