MSBuild 命令列-C++MSBuild on the command line - C++

一般情況下,我們建議您使用 Visual Studio,來設定專案屬性,並叫用 MSBuild 系統。In general, we recommend that you use Visual Studio to set project properties and invoke the MSBuild system. 不過,您可以使用MSBuild直接從命令提示字元工具。However, you can use the MSBuild tool directly from the command prompt. 建置程序是由在專案檔 (.vcxproj),您可以建立和編輯資訊控制。The build process is controlled by the information in a project file (.vcxproj) that you can create and edit. 專案檔指定為基礎的建置選項建置階段、 條件和事件。The project file specifies build options based on build stages, conditions, and events. 此外,您可以指定零或多個命令列選項引數。In addition, you can specify zero or more command-line options arguments.

msbuild.exe [ project_file ] [ options ]msbuild.exe [ project_file ] [ options ]

使用 /target (或 /t) 和 /property (或 /p) 命令列選項來覆寫特定的屬性和目標在專案檔中指定。Use the /target (or /t) and /property (or /p) command-line options to override specific properties and targets that are specified in the project file.

專案檔有一個重要功能是指定目標,這是套用至您的專案,並輸入和輸出執行該作業所需的特定作業。An essential function of the project file is to specify a target, which is a particular operation applied to your project, and the inputs and outputs that are required to perform that operation. 專案檔可以指定一或多個目標,其中可以包括預設目標。A project file can specify one or more targets, which can include a default target.

每個目標是由一或多個序列所組成任務Each target consists of a sequence of one or more tasks. 每個工作被以.NET Framework 類別,其中包含一個可執行命令。Each task is represented by a .NET Framework class that contains one executable command. 例如, CL 工作包含cl.exe命令。For example, the CL task contains the cl.exe command.

A工作參數是類別工作的屬性,通常代表可執行命令的命令列選項。A task parameter is a property of the class task and typically represents a command-line option of the executable command. 例如,FavorSizeOrSpeed的參數CL工作會對應至 /Os/Ot編譯器選項。For example, the FavorSizeOrSpeed parameter of the CL task corresponds to the /Os and /Ot compiler options.

其他工作參數都支援 MSBuild 基礎結構。Additional task parameters support the MSBuild infrastructure. 比方說,Sources工作參數指定一組可供其他工作的工作。For example, the Sources task parameter specifies a set of tasks that can be consumed by other tasks. 如需有關 MSBuild 工作的詳細資訊,請參閱工作參考For more information about MSBuild tasks, see Task Reference.

大部分工作都需要輸入和輸出,例如檔案名稱、 路徑和字串、 數值或布林參數。Most tasks require inputs and outputs, such as file names, paths, and string, numeric, or Boolean parameters. 比方說,一般輸入是編譯的.cpp 原始程式檔的名稱。For example, a common input is the name of a .cpp source file to compile. 重要的輸入的參數是一個字串,指定建置組態與平台,例如 「 偵錯|Win32"。An important input parameter is a string that specifies the build configuration and platform, for example, "Debug|Win32". 輸入和輸出由一或多個使用者定義的 XMLItem中所包含的項目ItemGroup項目。Inputs and outputs are specified by one or more user-defined XML Item elements contained in an ItemGroup element.

專案檔也可以指定使用者定義屬性ItemDefinitionGroup項目A project file can also specify user-defined properties and ItemDefinitionGroup items. 屬性和項目會形成名稱/值組,可用來當做組建中的變數。Properties and items form name/value pairs that can be used as variables in the build. 定義一組的名稱元件巨集,和值的部分宣告巨集值The name component of a pair defines a macro, and the value component declares the macro value. 屬性巨集可使用 $(名稱) 表示法和項目巨集使用存取 %(名稱) 標記法。A property macro is accessed by using $(name) notation, and an item macro is accessed by using %(name) notation.

在專案檔中的其他 XML 項目可以測試巨集,然後有條件地設定任何巨集的值或控制組建的執行。Other XML elements in a project file can test macros, and then conditionally set the value of any macro or control the execution of the build. 巨集名稱和常值字串可以串連來產生建構,例如路徑和檔案名稱。Macro names and literal strings can be concatenated to generate constructs such as a path and file name. 在命令列 /property選項可以設定或覆寫專案屬性。On the command line, the /property option sets or overrides a project property. 在命令列上,不能參考項目。Items cannot be referenced on the command line.

MSBuild 系統可以有條件地目標之前或之後執行另一個目標。The MSBuild system can conditionally execute a target before or after another target. 此外,系統可以建置目標,根據目標所取用的檔案是否比更新的檔案就會發出。Also, the system can build a target based on whether the files that the target consumes are newer than the files it emits.

如需 MSBuild 的詳細資訊,請參閱:For more information about MSBuild, see:

本節內容In This Section

詞彙Term 定義Definition
逐步解說:使用 MSBuild 建立 C++ 專案Walkthrough: Using MSBuild to Create a C++ Project 示範如何建立 Visual StudioC++使用的專案MSBuildDemonstrates how to create a Visual Studio C++ project using MSBuild.
如何:在 MSBuild 專案中使用建置事件How to: Use Build Events in MSBuild Projects 示範如何指定在組建中 particuler 階段,就會發生的動作: 在建置開始之前;連結步驟開始; 之前或在建置結束後。Demonstrates how to specify an action that occurs at a particuler stage in the build: before the build starts; before the link step starts; or after the build ends.
如何:將自訂建置步驟新增至 MSBuild 專案How to: Add a Custom Build Step to MSBuild Projects 示範如何將使用者定義的階段新增至組建順序。Demonstrates how to add a user-defined stage to the build sequence.
如何:將自訂建置工具新增至 MSBuild 專案How to: Add Custom Build Tools to MSBuild Projects 示範如何建置工具相關聯的特定檔案。Demonstrates how to associate a build tool with a particular file.
如何:將自訂工具整合至專案屬性中How to: Integrate Custom Tools into the Project Properties 示範如何將自訂工具的選項新增至專案屬性。Demonstrates how to add options for a custom tool to the project properties.
如何:修改目標 Framework 和平台工具組How to: Modify the Target Framework and Platform Toolset 示範如何編譯專案,以供多個架構或工具組。Demonstrates how to compile a project for multiple frameworks or toolsets.

另請參閱See also

從命令列使用 MSVC 工具組Use the MSVC toolset from the command line