How to: Use Build Events in MSBuild Projects (original) (raw)
A build event is a command that MSBuild performs at a particular stage in the build process. The pre-build event occurs before the build starts; the pre-link event occurs before the link step starts; and the post-build event occurs after the build successfully ends. A build event occurs only if the associated build step occurs. For example, the pre-link event does not occur if the link step does not run.
Each of the three build events is represented in an item definition group by a command element (<Command>) that is executed and a message element (<Message>) that is displayed when MSBuild performs the build event. Each element is optional, and if you specify the same element multiple times, the last occurrence takes precedence.
An optional use-in-build element (<build-eventUseInBuild>) can be specified in a property group to indicate whether the build event is executed. The value of the content of a use-in-build element is either true or false. By default, a build event is executed unless its corresponding use-in-build element is set to false.
The following table lists each build event XML element:
| XML Element | Description |
|---|---|
| PreBuildEvent | This event executes before the build begins. |
| PreLinkEvent | This event executes before the link step begins. |
| PostBuildEvent | This event executes after the build finishes. |
The following table lists each use-in-build element:
| XML Element | Description |
|---|---|
| PreBuildEventUseInBuild | Specifies whether to execute the pre-build event. |
| PreLinkEventUseInBuild | Specifies whether to execute the pre-link event. |
| PostBuildEventUseInBuild | Specifies whether to execute the post-build event. |
Example
The following example can be added inside of the Project element of the myproject.vcxproj file created in Walkthrough: Using MSBuild to Create a C++ Project. A pre-build event makes a copy of main.cpp; a pre-link event makes a copy of main.obj; and a post-build event makes a copy of myproject.exe. If the project is built using a release configuration, the build events are executed. If the project is built using a debug configuration, the build events are not executed.
<ItemDefinitionGroup>
<PreBuildEvent>
<Command>copy <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>P</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>D</mi><mi>i</mi><mi>r</mi><mo stretchy="false">)</mo><mi>m</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi mathvariant="normal">.</mi><mi>c</mi><mi>p</mi><mi>p</mi></mrow><annotation encoding="application/x-tex">(ProjectDir)main.cpp </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">ro</span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mclose">)</span><span class="mord mathnormal">main</span><span class="mord">.</span><span class="mord mathnormal">c</span><span class="mord mathnormal">pp</span></span></span></span>(ProjectDir)copyOfMain.cpp</Command>
<Message>Making a copy of main.cpp </Message>
</PreBuildEvent>
<PreLinkEvent>
<Command>copy <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>P</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>D</mi><mi>i</mi><mi>r</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(ProjectDir)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">ro</span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mclose">)</span></span></span></span>(Configuration)\main.obj <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>P</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>D</mi><mi>i</mi><mi>r</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(ProjectDir)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">ro</span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mclose">)</span></span></span></span>(Configuration)\copyOfMain.obj</Command>
<Message>Making a copy of main.obj</Message>
</PreLinkEvent>
<PostBuildEvent>
<Command>copy <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>P</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>D</mi><mi>i</mi><mi>r</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(ProjectDir)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">ro</span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mclose">)</span></span></span></span>(Configuration)\$(TargetFileName) <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>P</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>D</mi><mi>i</mi><mi>r</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(ProjectDir)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">ro</span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mclose">)</span></span></span></span>(Configuration)\copyOfMyproject.exe</Command>
<Message>Making a copy of myproject.exe</Message>
</PostBuildEvent>
</ItemDefinitionGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<PreBuildEventUseInBuild>true</PreBuildEventUseInBuild>
<PreLinkEventUseInBuild>true</PreLinkEventUseInBuild>
<PostBuildEventUseInBuild>true</PostBuildEventUseInBuild>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<PreBuildEventUseInBuild>false</PreBuildEventUseInBuild>
<PreLinkEventUseInBuild>false</PreLinkEventUseInBuild>
<PostBuildEventUseInBuild>false</PostBuildEventUseInBuild>
</PropertyGroup>
See also
MSBuild on the command line - C++
Walkthrough: Using MSBuild to Create a C++ Project