2014-08-30 59 views
2

我试过为编译器传递-ffunction-sections -fdata-sections,但这似乎并没有达到预期的效果。据我所知,我也必须通过-Wl,--gc-sections到链接器,但我现在没有链接文件。我只想让.a库文件尽可能小,并使用最少的冗余代码/数据。Code :: Blocks + MinGW:最小化静态库的大小

+0

没有最终的可执行文件,您希望从静态库中删除哪些代码?只有当可执行文件与静态库链接时才可以删除未使用的代码。看起来这个问题没有答案。 – 2014-09-02 09:37:47

+0

@AlexFarber的事情是,在玩编译器的命令行参数时,我记得能够将'.a'文件的大小减少〜50%,但现在我无法再现它。可执行文件也较小,并且它可以工作,所以必须有一些组合才能生成较小的静态库文件。 – Paul 2014-09-02 21:14:23

+0

'-Os -fvisibility = hidden -fomit-frame-pointer -mno-accumulate-outgoing-args -finline-small-functions -s';然而'-f * -sections'和'-flto'标志会使你的静态库变大(但是产生的二进制变小)......如果我不认为这个问题本身是错误的,我会提供一个完整的答案。真正使静态库变小的唯一方法是使用'--print-gc-sections'编译最终产品,以找出哪些部分是残缺的。然后返回并删除这些函数/变量(这也适用于制作较小的共享库 - 请参阅http://libraryopt.sf.net) – technosaurus 2014-09-05 02:58:05

回答

1

下面将减少你的编译对象的大小(因而静态库)

-Os -g0 -fvisibility=hidden -fomit-frame-pointer -mno-accumulate-outgoing-args -finline-small-functions -fno-unwind-tables -fno-asynchronous-unwind-tables -s

下面将增加物体的大小(尽管他们可能使代码更小)

-ffunction-sections -fdata-sections -flto -g -g1 -g2 -g3

真正使静态库变小的唯一方法是通过编译静态库-ffunction-sections -fdata-sections并链接来删除不需要的代码用-Wl,--gc-sections,--print-gc-sections的最终产品找出哪些部件是残余的。然后返回并移除这些函数/变量(这也适用于制作较小的共享库 - 请参阅http://libraryopt.sf.net

3

编译器根据它对程序的知识执行优化。尤其是,优化级别-O2及以上,特别是启用时间单位模式,这允许编译器在编译函数时考虑从文件中后面的函数获得的信息。一次将多个文件一次编译为单个输出文件允许编译器在编译每个文件时使用从所有文件中获得的信息。

不是所有优化都直接由一个标志控制。

-ffunction-sections -fdata-sections 如果目标支持任意部分,则将每个函数或数据项放置到输出文件的其自己的部分中。函数的名称或数据项的名称决定输出文件中该部分的名称。

Use these options on systems where the linker can perform optimizations to improve locality of reference in the instruction space. Most systems using the ELF object format and SPARC processors running Solaris 2 have linkers with such optimizations. AIX may have these optimizations in the future. 

Only use these options when there are significant benefits from doing so. When you specify these options, the assembler and linker will create larger object and executable files and will also be slower. You will not be able to use gprof on all systems if you specify this option and you may have problems with debugging if you specify both this option and -g. 

U可以使用下面的链接了解详情.. :-) http://gcc.gnu.org/onlinedocs/gcc-4.0.4/gcc/Optimize-Options.html

0

如果-g在编译器标志,VS没有它比较库的大小。如果包含调试信息,我很容易想象如果它包含调试信息,则将其大小加倍。如果这是您所看到的,那么很可能您已经剥离了调试符号库,因此无法显着缩小库文件。这可以解释你在某个时候将尺寸减半的记忆。