最近用了 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 。