最近用了 include-what-you-use 这个工具,分享一下
https://yodalee.me/2020/10/2020_iwyu/
大家都知道,程式不是写完就算了,是会长大跟更新
这时候 include 就会慢慢过时,可能本来需要的 include 现在不需要了
但通常在改程式码时不会意识到这点
如果参考 Google 的 cpp coding guide,会看到 Include What You Use 这条准则:
If a source or header file refers to a symbol defined elsewhere, the file
should directly include a header file which properly intends to provide a
declaration or definition of that symbol. It should not include header files
for any other reason.
原始码档案和标头档所需的符号,都应该引入适当的标头档来提供宣告或定义;
不能因为其他理由而引入标头档。
Do not rely on transitive inclusions. This allows people to remove
no-longer-needed #include statements from their headers without breaking
clients. This also applies to related headers - foo.cc should include bar.h
if it uses a symbol from it even if foo.h includes bar.h.
不可依赖过渡引入。开发者可以随时移除不需要的引入,又不会破坏客户端的相依性;
这也适用于相关的标头档:即使 foo.h 已经引入 bar.h,foo.cc 还是要引入 bar.h 。