ctrl-alt-delor
ctrl-alt-delor

Reputation: 7735

visual studio, build configuration: how to make a build configuration that is based on another

I wish to add a new build-configuration to by C# visual studio project. I would like it be be like the debug build-configuration, but with one difference. I would like it to always be like the debug configuration even when the debug configuration changes.

How do I do this?

Upvotes: 0

Views: 1113

Answers (1)

stijn
stijn

Reputation: 35901

Here's an example for using different preprocessor definitions. You'll have to manually edit the project file. I suggest you do this in VS itself since it has syntax highlighting and autocomplete for it. In a normal csproj file, the properties for the Debug|AnyCPU config are defined like this (1):

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <PlatformTarget>AnyCPU</PlatformTarget>
  <DebugType>pdbonly</DebugType>
  <Optimize>false</Optimize>
  <OutputPath>bin\Debug\</OutputPath>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
</PropertyGroup>

Say you want to reuse everything except DefineConstants, you create a seperate project file debug.props just for defining common properties, put it in the same directory as the project file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
</Project>

Then it's just a matter of adjusting the main project file to import the common file and set some different values based on configuration. This is done by replacing (1) with this:

<Import Project="$(MsBuildThisFileDirectory)\debug.props"
   Condition="'$(Configuration)'=='Debug' Or '$(Configuration)'=='MyDebug'" />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <DefineConstants>DEBUG</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'MyDebug|AnyCPU' ">
  <DefineConstants>TRACE;DEBUG</DefineConstants>
</PropertyGroup>

It should be fairly clear what this does: it imports the file with the common properties (if the config is Debug or MyDebug) then sets different values for DefineConstants depending on which Configuration is used. Since there's now a PropertyGroup for Configuration==MyDebug, VS will recignize this automatically so in the Configuration Manager you can now select MyDebug as Configuration. Once you do that, it effects the code like this:

#if TRACE //is now only defined for MyDebug config, not for Debug
Console.WriteLine( "hello there" );
#endif

Upvotes: 1

Related Questions