使用GCC/GNU-ld删除dead code

[url]
https://gcc.gnu.org/ml/gcc-help/2003-08/msg00128.html
https://embeddedfreak.wordpress.com/2009/02/10/removing-unused-functionsdead-codes-with-gccgnu-ld/
[/url]


[quote]
Hi Dr. Rupp,

I was just looking into this very thing!

Do this in your Makefile...
--------8<--------
DEADCODESTRIP := -Wl,-static -fvtable-gc -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,-s

foo : foo.c
g++ $(DEADCODESTRIP) $< -o $@
--------8<--------

Step by step...

-Wl,-static
Link against static libraries. Required for dead-code elimination.

-fvtable-gc
C++ virtual method table instrumented with garbage collection information for the linker.

-fdata-sections
Keeps data in separate data sections, so they can be discarded if unused.

-ffunction-sections
Keeps funcitons in separate data sections, so they can be discarded if unused.

-Wl,--gc-sections
Tell the linker to garbage collect and discard unused sections.

-s
Strip the debug information, so as to make the code as small as possible. (I presume that you'd want to do this in a dead-code removal build.)

The requirement to link against the static libraries was surprising to me. But there you go.

HTH,
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值