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?
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:
- How to remove unused C/C++ symbols with GCC and ld?
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).
精彩评论