c - 优化C编译: removed unreferenced parts on-the-fly

标签 c compilation linker optimization

我们正面临一个有趣的话题。假设我们有一个 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/

相关文章:

c++ - 错误 : no such file or directory: '–std=c++11' after compiling makefile

java - 我在 Netbeans 中的 java 项目变得很慢

python - 编译错误 - 无法导入名称 'gpio'

ios - Google Analytics 链接器错误。找不到 -lGGLAnalytics 的库

gcc - 巧妙使用链接描述文件?

c - unsigned char 和 char 异或运算

c - 在 C 中打开文件 - 为什么它不加载所有矩阵或删除\n?

c - fun() 的时间复杂度是多少?

c - 如何将 char 连接到常量 char*?

c++ - 使用 bjam 编译的 C++ 中的 undefined symbol