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

前端 未结 11 2307
北荒
北荒 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:31

    You'll want to check your docs for your version of gcc & ld:

    However for me (OS X gcc 4.0.1) I find these for ld

    -dead_strip
    

    Remove functions and data that are unreachable by the entry point or exported symbols.

    -dead_strip_dylibs
    

    Remove dylibs that are unreachable by the entry point or exported symbols. That is, suppresses the generation of load command commands for dylibs which supplied no symbols during the link. This option should not be used when linking against a dylib which is required at runtime for some indirect reason such as the dylib has an important initializer.

    And this helpful option

    -why_live symbol_name
    

    Logs a chain of references to symbol_name. Only applicable with -dead_strip. It can help debug why something that you think should be dead strip removed is not removed.

    There's also a note in the gcc/g++ man that certain kinds of dead code elimination are only performed if optimization is enabled when compiling.

    While these options/conditions may not hold for your compiler, I suggest you look for something similar in your docs.

提交回复
热议问题