Remove useless files, includes, global variables a

2019-04-09 16:38发布

问题:

I modified a huge C++ project with lots of files and functions. The problem is, that now there are tons of useless files, includes, global variables and functions. Removing them by hand would be a pain. Is there a tool that analyzes the code like a compiler does and deletes all unused stuff? I would prefer a tool for unix. Also a way to remove only one or a few of the useless components named above would help.

回答1:

There several posibilities of the GNU toolchain itself to optimize codesize, if you don't mind that the linker does this every time you build your system. And there is always the question in C++ what really is "unused code" (since working with pointers and casts can mislead any tool).

So your best bet for this is the Gold linker (Replacing ld with gold - any experience?) and the following options:

  • Remove unused sections -gc-sections: GCC --gc-sections and finding symbol dependencies
  • Instruction folding --icf: GCC(/Clang): Merging functions with identical instructions (COMDAT folding)
  • Optimize for Size -Os: Process for reducing the size of a executable

The "bigger" approach would be static code analyers/code refactoring tools (How can I know which parts in the code are never used?) and then certain libraries like Boost do come with their own tools to reduce the number of files.