共用方式為


在 Visual Studio 中設定 C/C++ Include Cleanup

從 17.8 Preview 1 開始,Visual Studio 可以清除您的 #include ,以下列方式改善 C 和 C++ 程式碼的品質:

  • 僅因為需要標頭檔會由另一個標頭檔間接包含,因此提供新增程式碼的標頭檔。
  • 移除未使用的標頭檔 -- 改善建置時間的供應專案。

本文說明如何在 Visual Studio 中設定 Include Cleanup。 如需 Include Cleanup 的詳細資訊,請參閱 C/C++ Include Cleanup 概觀

開啟 [包含清除]

[包含清除] 功能預設為開啟。 如果它未使用中,您可以透過 [工具 > 選項 > ] 文字編輯器 > C/C++ > 程式碼清除 開啟它,然後選取 [ 啟用 #include 清除]。

然後使用下拉式清單來設定您想要如何收到關於新增間接標頭或移除未使用標頭的機會通知:

The Tools options dialog opened at Text Editor > C/C++ > Code Cleanup.

核取 [啟用 # include cleanup] 核取方塊。 [移除未使用] 的下拉式清單包含建議層級,並顯示 [新增遺漏] 包含建議層級。 下拉式清單的內容會顯示,也就是:**重構僅限**、**建議**、**警告**和**錯誤**。 [移除未使用的包含建議層級] 下拉式清單提供相同的選項,但也會新增暗灰色。

建議層級選項的意義如下:

重構:包含清除提供動作,您可以在將滑鼠指標停留在 上方 #include 時,透過快速動作功能表採取動作,或將游標放在行上 #include ,然後按 Ctrl+句點:

A screenshot of the quick action to remove an unused header

將游標暫留在 #include iostream 上方時,燈泡會出現,其中包含 iostream 的文字不會用於此檔案中。

建議、警告、錯誤 :包含清除提供可透過 [錯誤清單] 視窗中的建議、警告或錯誤所採取的動作。 您可以判斷哪一個。 在下列錯誤清單的螢幕擷取畫面中,[包含清除] 已設定為顯示未使用的標頭,並出現警告。 確定 已選取下拉式清單中的 [建置 + Intellisense ],讓您可以看到 [包含清除] 輸出:

A screenshot of the Error List window.

下拉式清單篩選準則設定為 [建置 + IntelliSense]。 會顯示警告:VCIC002 - #include < iostream >不會用於此檔案中。

變 暗

在程式碼編輯器中,將未使用的標頭檔行變暗,以顯示未使用的標頭。 將游標暫留在暗灰色 #include 上方以顯示快速動作功能表,然後選擇 [顯示潛在的修正程式 ],或按一下燈泡下拉式清單,以查看與未使用檔案相關的動作。

A screenshot of a dimmed #include < iostream > line.

#include < iostream > 行會變暗,因為使用 iostream 的程式程式碼會批註化。該程式程式碼是 // std::cout < < 「charSize = 」 < < charSize;此行也會顯示快速動作功能表。 它表示 #include < iostream >不會用於此檔案,而且有顯示潛在修正的連結。

使用 設定 Include Cleanup .editorconfig

設定 Include Cleanup 有更多選項,例如從清除建議中排除指定的 include,指出需要某些標頭檔,讓工具不會將它們標示為未使用,依此類提。 這些選項定義在檔案中 .editorconfig ,您可以新增至專案,以及其他專案,針對在程式碼基底中運作的每個人強制執行一致的編碼樣式。 如需將檔案新增 .editorconfig 至專案的詳細資訊,請參閱 使用 EditorConfig 建立可攜式自訂編輯器設定。

.editorconfig您可以搭配 Include Cleanup 使用的設定如下:

設定 範例
cpp_include_cleanup_add_missing_error_tag_type

設定新增可轉移包含訊息的錯誤層級。
none
suggestion
warning
error
cpp_include_cleanup_add_missing_error_tag_type = suggestion
cpp_include_cleanup_remove_unused_error_tag_type

設定移除未使用之 Include 訊息的錯誤層級。
none
suggestion
warning
error
dimmed
cpp_include_cleanup_remove_unused_error_tag_type = dimmed
cpp_include_cleanup_excluded_files

從包含清除訊息中排除指定的檔案。 您完全不會收到與標頭相關的建議,無論是新增標頭還是未使用。
檔案名稱 cpp_include_cleanup_excluded_files = vcruntime.h, vcruntime_string.h
cpp_include_cleanup_required_files

指定 file1 的使用 方式需要 file2 。 例如,指定如果您使用 atlwin.haltbase.h 也必須包含該內容。
file1 file2 cpp_include_cleanup_required_files = atlwin.h:altbase.h, atlcom.h:altbase.h
cpp_include_cleanup_replacement_files

Include Cleanup 處理期間,將 file1 取代為 file2 。 例如,您可能偏好使用 cstdio 而不是 stdio.h 。 如果您有具有 #include <cstudio> 和 的 #include <stdio.h> 檔案,而且您只取用 來自 stdio.h 的內容,且此設定 Include Cleanup 會告訴您移除 stdio.h ,因為它取代了 處理期間 使用 的 stdio.h 用法 cstdio 。 如果您沒有使用其中一項的內容,則 Include Cleanup 會告訴您要移除這兩者。
file1 file2 cpp_include_cleanup_replacement_files = stdio.h:cstdio,stdint.h:cstdint
cpp_include_cleanup_alternate_files

如果 包含 file1 ,請勿為間接 include file2 產生訊息。 例如,如果您 #include <windows.h> 且只使用其間接包含標頭 winerror.h 中的專案,則 Include Cleanup 不會提示新增 winerror.h 。 當您偏好包含外觀標頭檔,而不是間接包含它時,很有用。
file1 file2 cpp_include_cleanup_alternate_files = windows.h:winerror.h, windows.h:minwindef.h

另請參閱

C/C++ 包含清除概觀
包含清除訊息