December 20, 2014
Hot Topics:

Exploring the New MSBuild Features for Visual C++ 2010

  • February 2, 2009
  • By Nick Wienholt
  • Send Email »
  • More Articles »

The move in Visual C++ 2010 to MSBuild is a great step forward for Visual C++ developers. At first glance, the use of a different file format for Visual C++ projects may cause reactions ranging from indifference to concern over disruptions and breakages to any existing customizations to the build process. However, by allowing Visual C++ builds to take advantage of the wide variety of extensibility points in the MSBuild engine, the limited extensibility of the VCPROJ format looks primitive by comparison.

MSBuild: The Basics

In some ways, the use of MSBuild as the file format for Visual C++ is a back-to-the-future moment for long-time C++ developers. In the early days of Visual C++, the MAK file format was preferred way to describe the build process that would compile and link a collection of source code files into a binary image. Although MAK files and the nmake tool are still supported, the file format of Visual C++ changed over the years to an XML format that was not compatible with nmake. To support Visual C++ compilations on build boxes and other machines without the Visual C++ IDE installed, VCBuild.exe was introduced; it can build based on the Visual C++ XML VCPROJ file format. A degree of MSBuild support is available in Visual C++ 2008 and below with the VBBuild MSBuild task, but this treats the Visual C++ compilation as a black box, and does not allow easy integration with the rest of MSBuild’s features.

Visual C++ 2010 uses MSBuild as the native project file format for C++. This offers a number of advantages for C++ developers—MSBuild is extremely flexible in the ways a build process can be customized, and custom task development for MSBuild is simple (tasks are the 'doing' things in MSBuild, and they will be covered in more details later in this article). Microsoft ships dozens of MSBuild Tasks out-of-the-box, with functionality ranging from copying files to registering assemblies to signing a file with a certificate. There is also a rich range of community and commercial tasks available for more advanced tasks, and the Exec task can be used to call out to existing command-line based build tools.

The Visual C++ 2010 October 2008 CTP ships with MSBuild support disabled, but it can be activated by running the NewVcProjectSystem.cmd batch file located at C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE on the Virtual PC image. It also is possible to upgrade an existing project to use the MSBuild file format using the C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools\vcupgrade.exe application. The RTM version of Visual C++ 2010 will not have this issue.

MSBuild Features

The full schema definition for the MSBuild file format is available on the MSDN site. Even though the schema looks pretty complex, MSBuild conceptually can be simplified to four main concepts:

Items

An item in MSBuild is an input into the build system. Items are specified in MSBuild by including them as child elements of an ItemGroup element. The name of the elements beneath an ItemGroup determines the name of the ItemGroup. The following XML sample shows three of the ItemGroup elements from a Win32Console application:

<ItemGroup>
   <None Include="ReadMe.txt" />
</ItemGroup>

<ItemGroup>
   <ClInclude Include="stdafx.h" />
   <ClInclude Include="targetver.h" />
</ItemGroup>

<ItemGroup>
   <ClCompile Include="stdafx.cpp">
      <PrecompiledHeader
         Condition="'$(Configuration)|$(Platform)'==
         'Debug|Win32'">Create</PrecompiledHeader>
      <PrecompiledHeader
         Condition="'$(Configuration)|$(Platform)'==
         'Release|Win32'">Create</PrecompiledHeader>
   </ClCompile>
   <ClCompile Include="Win32ConsoleApp.cpp" />
</ItemGroup>

The three ItemGroups are None, ClInclude, and ClCompile. These item groups can be referred to in the MSBuild file by using the @(group_name) syntax. For the MSbuild extract shown, the three item group names are @(None), @(ClInclude), and @(ClCompile). The @(ClCompile) item also includes a child element that contains metadata about the item. Item metadata is referenced by prefixing it with the percent (%) symbol, so @(ClCompile) has metadata of %PrecompiledHeader.

Properties

Although Items allow complex collections of build input to be specified, MSBuild Properties are much simpler, and are used to specify a simple key-value pair of information for the build. The properties in a MSBuild file map to the properties that are set on the Configuration properties for a Visual C++ project. As shown below, properties are grouped in a PropertyGroup element in much the same way as ItemGroup elements.

Some properties will be relevant regardless of build configuration; others will be relevant only for a certain project configuration. In the following MSBuild extract, two property groups are shown—the first property group is relevant for all project configurations, and the second is conditionally active for a Debug | Win32 configuration. Properties can be referred to in other sections of the MSBuild file by using the $(PropertyName) syntax, as shown for the $(Configuration) and $(Platform) properties in the Condition statement.

<PropertyGroup>
   <ConfigurationType>Application</ConfigurationType>
   <TargetName>Win32ConsoleApp</TargetName>
   <ShowAllFiles>false</ShowAllFiles>
   <ProjectGUID>
      {A3D3BF50-460A-4396-9204-7E4041E9AD7B}
   </ProjectGUID>
   <TargetFrameworkVersion>4.0</TargetFrameworkVersion>
   <Keyword>Win32Proj</Keyword>
   <RootNamespace>Win32ConsoleApp</RootNamespace>
</PropertyGroup>

<PropertyGroup
   Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
   <CharacterSet>Unicode</CharacterSet>
   <LinkIncremental>true</LinkIncremental>
</PropertyGroup>

Properties also can be specified on the MSBuild command line by using the /property command line switch. It has the effect of overriding the value of the property set within the MSBuild file.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel