🚀 在 VS Code 中

自訂預設設定

您可以覆寫在 c_cpp_properties.json 中設定之屬性的預設值。

Visual Studio Code 設定

下列 C_Cpp.default.* 設定對應至 c_cpp_properties.json 設定區塊中的每個屬性。即

C_Cpp.default.includePath                          : string[]
C_Cpp.default.defines                              : string[]
C_Cpp.default.compileCommands                      : string
C_Cpp.default.macFrameworkPath                     : string[]
C_Cpp.default.forcedInclude                        : string[]
C_Cpp.default.intelliSenseMode                     : string
C_Cpp.default.compilerPath                         : string
C_Cpp.default.compilerArgs                         : string[]
C_Cpp.default.configurationProvider                : string
C_Cpp.default.customConfigurationVariables         : object | null
C_Cpp.default.cStandard                            : c89 | c99 | c11 | c17
C_Cpp.default.cppStandard                          : c++98 | c++03 | c++11 | c++14 | c++17 | c++20 | c++23
C_Cpp.default.enableConfigurationSquiggles         : boolean
C_Cpp.default.mergeConfigurations                  : boolean
C_Cpp.default.systemIncludePath                    : string[]
C_Cpp.default.windowsSdkVersion                    : string
C_Cpp.default.browse.path                          : string[]
C_Cpp.default.browse.defines                       : string[]
C_Cpp.default.browse.dotConfig                     : string
C_Cpp.default.browse.databaseFilename              : string
C_Cpp.default.browse.limitSymbolsToIncludedHeaders : boolean

這些設定具有 VS Code 設定的所有優點,表示它們可以具有預設值、「使用者」、「工作區」和「資料夾」值。因此,您可以在「使用者」設定中為 C_Cpp.default.cppStandard 設定全域值,並使其套用至您開啟的所有資料夾。如果任何一個資料夾需要不同的值,您可以透過新增「資料夾」或「工作區」值來覆寫該值。

VS Code 設定的此屬性可讓您獨立設定每個工作區,從而使 c_cpp_properties.json 檔案成為選用項目。

已更新的 `c_cpp_properties.json` 語法

已將特殊變數新增至 c_cpp_properties.json 的接受語法,這會指示擴充功能插入上述 VS Code 設定中的值。如果您將 c_cpp_properties.json 中任何設定的值設定為 "${default}",它會指示擴充功能讀取該屬性的 VS Code 預設設定並插入它。例如

"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "additional/paths",
            "${default}"
        ],
        "defines": [
            "${default}"
        ],
        "macFrameworkPath": [
            "${default}",
            "additional/paths"
        ],
        "forcedInclude": [
            "${default}",
            "additional/paths"
        ],
        "compileCommands": "${default}",
        "browse": {
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": "${default}",
            "path": [
                "${default}",
                "additional/paths"
            ]
        },
        "intelliSenseMode": "${default}",
        "cStandard": "${default}",
        "cppStandard": "${default}",
        "compilerPath": "${default}"
    }
],

請注意,對於接受 string[] 的屬性,上述語法可讓您使用其他值來擴增 VS Code 設定,因此可讓您在 VS Code 設定中列出常見路徑,並在 c_cpp_properties.json 中列出組態特定的設定。

如果 c_cpp_properties.json 中缺少屬性,擴充功能將使用 VS Code 設定中的值。如果開發人員為適用於指定資料夾的所有設定指派值,則可以從 .vscode 資料夾中移除 c_cpp_properties.json,因為不再需要它。

如需有關 c_cpp_properties.json 設定檔的深入資訊,請參閱c_cpp_properties.json 參考

系統包含

將新增一個新設定,讓您可以指定與資料夾包含路徑分開的系統包含路徑。如果此設定具有值,則擴充功能從 compilerPath 設定中指定的編譯器取得的系統包含路徑將不會新增至擴充功能用於 IntelliSense 的路徑陣列。我們可能想要提供 VS Code 命令,以從編譯器的預設值填入此值,以供有興趣使用它的使用者,以便他們想要對預設值進行一些修改。

C_Cpp.default.systemIncludePath : string[]

系統包含路徑/定義解析策略

擴充功能以下列方式判斷系統 includePath 和定義以傳送至 IntelliSense 引擎

  1. 如果 compileCommands 具有有效值,且編輯器中開啟的檔案位於資料庫中,請使用資料庫項目中的編譯命令來判斷包含路徑和定義。

    • 系統包含路徑和定義是使用下列邏輯判斷 (依序)
      1. 如果 systemIncludePath 具有值,請使用它 (繼續下一步以搜尋系統定義)。
      2. 如果 compilerPath 有效,請查詢它。
      3. 將命令中的第一個引數解譯為編譯器,並嘗試查詢它。
      4. 如果 compilerPath 為 "",請針對系統包含路徑和定義使用空陣列。
      5. 如果 compilerPath 未定義,請在系統上尋找編譯器並查詢它。
  2. 如果 compileCommands 無效,或目前檔案未列在資料庫中,請使用組態中用於 IntelliSense 的 includePathdefines 屬性。

    • 系統包含路徑和定義是使用下列邏輯判斷 (依序)
      1. 如果 systemIncludePath 具有值,請使用它 (繼續下一步以搜尋系統定義)。
      2. 如果 compilerPath 有效,請查詢它。
      3. 如果 compilerPath 為 "",請針對系統包含路徑和定義使用空陣列 (假設它們已在目前組態的 includePathdefines 中)。
      4. 如果 compilerPath 未定義,請在系統上尋找編譯器並查詢它。

系統包含不應新增至 includePathbrowse.path 變數。如果擴充功能在 includePath 屬性中偵測到任何系統包含路徑,它會靜默移除它們,以便它可以確保系統包含路徑最後新增且順序正確 (這對於 GCC/Clang 尤其重要)。

增強的語意色彩化

啟用 IntelliSense 時,Visual Studio Code C/C++ 擴充功能支援語意色彩化。如需有關設定類別、函式、變數等等色彩的詳細資訊,請參閱增強的色彩化

擴充功能記錄

如果您在使用擴充功能時遇到問題,而我們無法根據您的問題報告中的資訊進行診斷,我們可能會要求您啟用記錄並將您的記錄傳送給我們。如需有關如何收集記錄的資訊,請參閱C/C++ 擴充功能記錄