How do I include only used symbols when statically linking with gcc?

前端 未结 1 1707
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 05:23

I\'m deploying a small program compiled with gcc, 4.3.2-1.1 (Debian). This program will be deployed on virtual machine templates ranging from Debain 5 to bleeding edge Fedor

相关标签:
1条回答
  • 2020-12-13 06:03

    This issue is related not only to gcc, but to ld(1) too.

    By default, gcc doesn't eliminate dead code, you can check this by compiling/linking executable, and then running

    objdump -d a.out

    which shows you all functions in your executable.

    Simple "googling" give this link.

    So, to remove unused functions, you need:

    • 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.
    0 讨论(0)
提交回复
热议问题