我们正面临一个有趣的话题。假设我们有一个 special-functions.c 文件,基本上是一个库。 我们需要优化代码,在构建过程中即时删除所有未使用/未引用的函数。 我不是在搜索通常未使用的(死的)代码:在编译为其中一种架构的情况下,某些部分将“死”,但它将用于其他架构构建。
有人知道执行此操作的标志、工具、方法和技巧吗? 编译器是带有 ANSI 99 C 代码的标准 gcc。
编辑 我知道,这主要是链接器的部分,但是使用 gcc,这个过程并没有真正分成两部分。
最佳答案
来自 http://embeddedfreak.wordpress.com/2009/02/10/removing-unused-functionsdead-codes-with-gccgnu-ld/ :
- Compile with
-fdata-sections
to keep the data in separate data sections and-ffunction-sections
to keep functions in separate sections, so they (data and functions) can be discarded if unused.- Link with
--gc-sections
to remove unused sections.
例如:
gcc -Os -fdata-sections -ffunction-sections test.c -o test -Wl,--gc-sections
关于c - 优化C编译: removed unreferenced parts on-the-fly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8338760/