How to remove unused C/C++ symbols with GCC and ld?

前端 未结 11 2417
北荒
北荒 2020-11-22 09:46

I need to optimize the size of my executable severely (ARM development) and I noticed that in my current build scheme (gcc + ld) unuse

11条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 10:09

    It seems to me that the answer provided by Nemo is the correct one. If those instructions do not work, the issue may be related to the version of gcc/ld you're using, as an exercise I compiled an example program using instructions detailed here

    #include 
    void deadcode() { printf("This is d dead codez\n"); }
    int main(void) { printf("This is main\n"); return 0 ; }
    

    Then I compiled the code using progressively more aggressive dead-code removal switches:

    gcc -Os test.c -o test.elf
    gcc -Os -fdata-sections -ffunction-sections test.c -o test.elf -Wl,--gc-sections
    gcc -Os -fdata-sections -ffunction-sections test.c -o test.elf -Wl,--gc-sections -Wl,--strip-all
    

    These compilation and linking parameters produced executables of size 8457, 8164 and 6160 bytes, respectively, the most substantial contribution coming from the 'strip-all' declaration. If you cannot produce similar reductions on your platform,then maybe your version of gcc does not support this functionality. I'm using gcc(4.5.2-8ubuntu4), ld(2.21.0.20110327) on Linux Mint 2.6.38-8-generic x86_64

提交回复
热议问题