问题
I need some help for compiling with GCC under MinGW.
Say I have 2 files: a.c contains 2 functions a1 and a2 b.c contains 2 functions b1 and b2.
Then I link the 2 objects into a shared library. The command used are like:
gcc -c a.c
gcc -c b.c
gcc -shared -Wl, --version-script v.ver -Wl, -Map=out.map -Wl, --strip-all -o mydll.dll a.o b.o
v.ver looks like:
mylib {
global: a1;
a2;
local: *;
}
which is used to control which functions to be exported.
By checking the mapfile I can see that the 2 functions in b.c are also included into the .text section of the dll.
Because this dll only exports a1 and a2, and b1 and b2 are only defined in b.c but never used anywhere. Is there any option I could add in gcc or ld so that b1 and b2 are not built into the dll so that I can save some space in the dll?
Thanks
回答1:
Yes, this is possible. To do this, add the following two flags when compiling your C source code to objects:
-ffunction-sections -fdata-sections
This will generate bigger object files, but will add a lot of information for the linker. When calling the linker add the following flag:
--gc-sections
The linker will now throw away all functions and sections that are not used.
See also this question: Query on -ffunction-section & -fdata-sections options of gcc for more information.
来源:https://stackoverflow.com/questions/36033595/discard-unused-functions-in-gcc