Share via


tasks.vs.json 架構參考 (C++)

若要告訴 Visual Studio 如何在 Open Folder 專案中建置原始程式碼,請新增 tasks.vs.json 檔案。 您可以在這裡定義任何任意工作,然後從 方案總管 操作功能表叫用它。 CMake 專案不會使用此檔案,因為 CMakeLists.txt 會指定所有建置命令。 對於 CMake 以外的組建系統, tasks.vs.json 是您可以指定建置命令並叫用建置腳本的位置。 如需使用 tasks.vs.json 的一般資訊,請參閱 自訂「開啟資料夾」開發的建置和偵錯工作

工作具有 type 屬性,其可能具有四個值之一: defaultlaunchremotemsbuild 。 除非需要遠端連線,否則大部分的工作都應該使用 launch

預設屬性

所有工作類型都有預設屬性:

屬性 類型​ 描述
taskLabel string (必要。)指定使用者介面中使用的工作標籤。
appliesTo string (必要。)指定命令可以執行的檔案。 支援使用萬用字元,例如:「 」、「 .cpp」、「/*.txt」
contextType string 允許的值:「custom」、「build」、「clean」、「rebuild」。 決定工作會出現在操作功能表中的位置。 預設為 「custom」。
output string 指定工作的輸出標記。
inheritEnvironments 陣列 指定繼承自多個來源的一組環境變數。 您可以在 CMake設定.json 或 CppProperties.json 檔案中定義變數,並將其提供給工作內容。 Visual Studio 16.4: :使用 env.VARIABLE_NAME 語法,依工作指定環境變數。 若要取消設定變數,請將它設定為 「null」。
passEnvVars boolean 指定是否要在工作內容中包含其他環境變數。 這些變數與使用 envVars 屬性定義的變數不同。 預設為 「true」。

啟動屬性

當工作類型為 launch 時,這些屬性可供使用:

屬性 類型​ 描述
command string 指定要啟動之進程或腳本的完整路徑。
args 陣列 指定傳遞至命令的引數逗號分隔清單。
launchOption string 允許的值:「None」、「ContinueOnError」、「IgnoreError」。 指定發生錯誤時,如何繼續進行命令。
workingDirectory string 指定將執行命令的目錄。 預設為專案的目前工作目錄。
customLaunchCommand string 指定要在執行命令之前套用的全域範圍自訂。 適用于設定環境變數,例如 %PATH%。
customLaunchCommandArgs string 指定 customLaunchCommand 的引數。 (需要 customLaunchCommand 。)
env 指定自訂環境變數的索引鍵/值清單。 例如,「myEnv」:「myVal」
commands 陣列 指定要依序叫用的命令清單。

範例

下列工作會在資料夾中提供 makefile 時叫 用 make.exe ,而 Mingw64 環境已在 CppProperties.json 定義,如 CppProperties.json 架構參考 所示

 {
  "version": "0.2.1",
  "tasks": [
    {
      "taskLabel": "gcc make",
      "appliesTo": "*.cpp",
      "type": "launch",
      "contextType": "custom",
      "inheritEnvironments": [
        "Mingw64"
      ],
      "command": "make"
    },
    {
      "taskLabel": "gcc clean",
      "appliesTo": "*.cpp",
      "type": "launch",
      "contextType": "custom",
      "inheritEnvironments": [
        "Mingw64"
      ],
      "command": "make",
      "args": ["clean"]
    }
  ]
}

當您以滑鼠右鍵按一下 方案總管 中的 .cpp 檔案時,可以從操作功能表叫用這些工作。

遠端屬性

當您使用 C++ 工作負載安裝 Linux 開發,並使用 Visual Studio 連線ion Manager 新增遠端電腦的連線時,就會啟用遠端工作。 遠端工作會在遠端系統上執行命令,也可以將檔案複製到遠端系統。

當工作類型為 remote 時,這些屬性可供使用:

屬性 類型​ 描述
remoteMachineName string 遠端電腦的名稱。 必須符合 連線ion Manager 中的 電腦名稱稱。
command string 要傳送至遠端電腦的命令。 根據預設,命令會在遠端系統上的 $HOME 目錄中執行。
remoteWorkingDirectory string 遠端電腦上的目前工作目錄。
localCopyDirectory string 要複製到遠端電腦的本機目錄。 預設為目前的工作目錄。
remoteCopyDirectory string 複製到遠端 localCopyDirectory 電腦上的目錄。
remoteCopyMethod string 要用於複製的方法。 允許的值:「none」、「sftp」、「rsync」。 建議針對大型專案使用 rsync。
remoteCopySourcesOutputVerbosity string 允許的值:「Normal」、「Verbose」、「Diagnostic」。
rsyncCommandArgs string 預設為 「-t --delete」。
remoteCopyExclusionList 陣列 localCopyDirectory 要從複製作業中排除的檔案逗號分隔清單。

範例

當您以滑鼠右鍵按一下 方案總管 中的 main.cpp 時,下列工作會出現在操作功能表中。 這取決於 連線ion Manager 中呼叫 ubuntu 遠端電腦。 工作會將 Visual Studio sample 中目前的開啟資料夾複製到遠端電腦上的目錄,然後叫用 g++ 來建置程式。

{
  "version": "0.2.1",
  "tasks": [
    {
      "taskLabel": "Build",
      "appliesTo": "main.cpp",
      "type": "remote",
      "contextType": "build",
      "command": "g++ main.cpp",
      "remoteMachineName": "ubuntu",
      "remoteCopyDirectory": "~/sample",
      "remoteCopyMethod": "sftp",
      "remoteWorkingDirectory": "~/sample/hello",
      "remoteCopySourcesOutputVerbosity": "Verbose"
    }
  ]
}

MSBuild 屬性

當工作類型為 msbuild 時,這些屬性可供使用:

屬性 類型​ 描述
verbosity string 指定 MSBuild 專案建置輸出 verbosityAllowed 值:「Quiet」、「Minimal」、「Normal」、「Detailed」、「Diagnostic」。
toolsVersion string 指定要建置專案的工具組版本,例如 「2.0」、「3.5」、「4.0」、「Current」。 預設為 「Current」。
globalProperties object 指定要傳入專案的全域屬性索引鍵/值清單,例如 「Configuration」:「Release」
properties object 只指定其他專案屬性的索引鍵/值清單。
targets 陣列 指定要依序在專案上叫用的目標清單。 如果未指定任何專案,則會使用專案的預設目標。