Sam在程序开发中.doc
文本预览下载声明
Sam在程序开发中,经常要用到多层库嵌套。 Sam常在想,一些底层库的符号(symbols),在上层库乃至应用程序中并非全部要使用到。如果这些符号(symbols)实现一层层报喊道上一层库中,并最终添加到应用程序中,这不是既增大了应用程序大小,也造成运行速度的降低?
记得GCC编译选项中有解决这个问题的方法。于是查找之。
0. 总体思想:
网络上有人建议如下设置:
1. 编译阶段使用:
-ffunction-sections
-fdata-sections
2. 链接阶段使用:
-Wl,--gc-sections
在GCC,LD官方文档中如下讲解:
-ffunction-sections
-fdata-sections
Place each function or data item into its own section in the output file if the target supports arbitrary sections. The name of the function or the name of the data item determines the sections name in the output file.
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.
--gc-sections
--no-gc-sections
Enable garbage collection of unused input sections. It is ignored on targets that do not support this option. The default behaviour (of not performing this garbage collection) can be restored by specifying `--no-gc-sections on the command line.
`--gc-sections decides which input sections are used by examining symbols and relocations. The section containing the entry symbol and all sections containing symbols undefined on the command-line will be kept, as will sections containing symbols referenced by dynamic objects. Note that when building shared libraries, the linker must assume that any visible symbol is referenced. Once this initial set of sections has been determined, the linker recursively marks as used any section referenced by their relocations. See `-
显示全部