删除 C++ 中无用的文件、包含、全局变量和函数

Remove useless files, includes, global variables and functions in C++

我修改了一个包含大量文件和函数的巨大 C++ 项目。问题是,现在有大量无用的文件,包括全局变量和函数。手动移除它们会很痛苦。有没有像编译器那样分析代码并删除所有未使用的东西的工具?我更喜欢 unix 的工具。另外,仅删除上面提到的一个或几个无用组件的方法也会有所帮助。

GNU 工具链本身有多种优化代码大小的可能性,如果您不介意每次构建系统时链接器都会这样做的话。在 C++ 中始终存在问题 "unused code" 到底是什么(因为使用指针和强制转换会误导任何工具)。

所以最好的选择是 Gold linker (Replacing ld with gold - any experience?) 和以下选项:

  • 删除未使用的部分-gc-sectionsGCC --gc-sections and finding symbol dependencies
  • 指令折叠--icfGCC(/Clang): Merging functions with identical instructions (COMDAT folding)
  • 优化尺寸 -OsProcess for reducing the size of a executable

"bigger" 方法将是静态代码 analyers/code 重构工具(How can I know which parts in the code are never used?) and then certain libraries like Boost do come with their own tools 以减少文件数量。