丢弃GCC中未使用的功能 [英] discard unused functions in GCC

查看:113
本文介绍了丢弃GCC中未使用的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MinGW下使用GCC进行编译时,我需要一些帮助.

I need some help for compiling with GCC under MinGW.

说我有2个文件: a.c包含2个函数a1和a2 b.c包含2个函数b1和b2.

Say I have 2 files: a.c contains 2 functions a1 and a2 b.c contains 2 functions b1 and b2.

然后,我将2个对象链接到共享库中.使用的命令如下:

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看起来像:

v.ver looks like:

mylib {
   global: a1;
           a2;
   local: *;
} 

用于控制要导出的功能.

which is used to control which functions to be exported.

通过检查映射文件,我可以看到b.c中的2个函数也包含在dll的.text部分中.

By checking the mapfile I can see that the 2 functions in b.c are also included into the .text section of the dll.

因为此dll仅导出a1和a2,而b1和b2仅在b.c中定义,但从未在任何地方使用.是否可以在gcc或ld中添加任何选项,以使b1和b2不会内置到dll中,从而可以在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?

谢谢

推荐答案

是的,这是可能的.为此,在将C源代码编译到对象时,添加以下两个标志:

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.

另请参阅以下问题:在-ffunction-section上查询& gcc的-fdata-sections选项以获取更多信息.

See also this question: Query on -ffunction-section & -fdata-sections options of gcc for more information.

这篇关于丢弃GCC中未使用的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆