#498 – Creating a New Solution Configuration

We saw how to create a new build configuration and, optionally, create a matching solution configuration.  You can also create a new solution configuration and, optionally, create new matching build configurations in each project.

To create a new solution configuration, first open the Configuration Manager.

Now click on the list of solution configurations and select <New…>

You’ll see the New Solution Configuration dialog.  Give the new configuration a name and pick a solution configuration that you want to copy settings from.

After pressing OK, you can set build configuration and platform choices for the new solution configuration.

In the example below, we’ve created a new configuration that uses the same build configuration choices as Debug but builds only the top-level application.

We now see our new solution configuration as a choice in the main solution configurations dropdown.

#497 – Creating a New Build Configuration in a Project

In Visual Studio, you manage both build configurations, for each project, and solution configurations.  The project-level configurations specify sets of build options.  The solution-level configurations specify which project-level configurations are associated with each solution configuration.

You create and edit both project-level and solution-level configurations from the Configuration Manager.

To create a new project-level configuration, you select <New…> from the Configuration dropdown for a specific project.

You then give the new configuration a name and, optionally, copy settings from an existing configuration.

By default, this will create an identically named solution-level configuration.  You can uncheck the Create new solution configurations option to avoid this.  There are cases when you don’t necessarily want a one-to-one mapping between solution-level and project-level configurations.  In the example below, we’ve created a DebugWithWarningsAsErrors option, set appropriate project properties, and then build only one project with this configuration.

#496 – Editing Solution Configurations with the Configuration Manager

You can use the Configuration Manager to view or edit solution configurations in Visual Studio 2010.

In the example below, we have a solution that contains two projects–a DogApplication project that generates an .exe (executable) and a DogLibrary project that generates a .dll (class library).  We’ve opened the Configuration Manager and selected Debug as the solution configuration and x86 as the platform.  We can see that both projects are set to use Debug as their build configuration and x86 as their platform.

We can, however, make a change so that the DogLibrary project is built using its Release configuration, even when the solution configuration is Debug.  We might do this so that we can debug the main application, but use an optimized build of the class library.

#495 – Viewing Solution Configurations with the Configuration Manager

You can edit a solution configuration by using the Configuration Manager.  You can bring up the configuration manager in several different ways.

Select it from build configuration or platform dropdowns:

From the Build menu:

Right-click the solution in Solution Explorer:

By default, the solution configuration will be mapped to corresponding build configuration and platform choices in the default child project.  For example, as you switch between Debug and Release in the solution configuration, you’ll get configurations in which the project’s build configuration is Debug or Release.

#494 – Selecting a Solution-Level Build Configuration and Platform

Visual Studio 2010 supports both solution-level and project-level build configurations.  The project-level configurations specify sets of build-related properties.  The solution-level configuration specifies which projects to build for a particular configuration, as well as the individual project-level configurations to use.

There are also both solution-level and project-level platform choices.

You can see your current solution-level build configuration and platform in the main Visual Studio 2010 toolbar.

In the example below, the current build configuration is Debug and the current platform is Any CPU.

To build your project under a different build configuration or platform, just make a new choice in one or both of these combo boxes.

In the example below, we’ve chosen to do a Release build for the x86 platform.

 

#493 – Project Properties Are Specific to Build Configuration and Platform

Many tabs in the project properties window include two combo boxes at the top of the tab, allowing selection of Configuration and Platform.

This window allows you to specify a set of property values for each combination of Configuration and Platform.  For example, you might have Debug and Release choices for Configuration, as well as x86 and x64 choices for Platform.  You could then specify a different value for Output path for each combination of Configuration and Platform, so that you could save four sets of binaries.

  • Debug|x86 – \bin\x86\Debug
  • Debug|x64 – \bin\x64\Debug
  • Release|x86 – \bin\x86\Release
  • Release|x64 – \bin\x64\Release

#492 – Define Your Own Conditional Compilation Symbol

You can define your own conditional compilation symbols for specific build configurations and then use the #if directive to check these symbols and conditionally compile code, depending on whether the symbol is defined or not.

For example, suppose you want to execute some debugging code only for Debug builds that are targeting the x86 platform.  You can define a new DEBUG86 symbol that is present only when the build configuration is Debug and the platform is x86.

You can then use the #if directive to compile some code only when this symbol is defined.

#if DEBUG86
            Console.WriteLine("This is a 32-bit Debug build..");
#endif

#490 – Using the DEBUG Conditional Compilation Symbol

By default, Visual Studio creates a Debug build configuration in which the Define DEBUG constant option is checked.

You can check to see if this constant (or “conditional compilation symbol”) is defined in your code, using the #if directive.

        public void Bark()
        {
#if DEBUG
            Console.WriteLine("DEBUG INFO: Entering Dog.Bark() [{0}]", Name);
#endif

            Console.WriteLine("{0} says woof", Name);

#if DEBUG
            Console.WriteLine("DEBUG INFO: Exiting Dog.Bark() [{0}]", Name);
#endif
        }

Code between the #if DEBUG and matching #endif will be compiled only if the DEBUG compilation symbol has been defined.  By default, this will be true for the Debug build configuration, but not the Release build configuration.

When we select the Debug build configuration and re-build, we see the following output:

When we select the Release build configuration, where the DEBUG symbol is no longer defined, we see the following output:

#489 – Debug and Release Configurations Are Created Automatically

When you create a new project, two build configurations are created for you automatically–Debug and Release.  The Debug configuration is meant to be used for debugging your application and the Release configuration is meant to be the one that you build with when releasing your application.

The Debug configuration

  • Defines the DEBUG constant (which you can use in your code to conditionally compile code only when debugging)
  • Does not optimize the code (to make stepping through the code in the debugger easier)
  • Builds output in \bin\Debug directory, rather than \bin\Release
  • Outputs full debug info, so that the appropriate source code lines can be located when debugging

#488 – Build Platforms Allow Project Properties to Target a Platform

Build Configurations allow you to build the project in different ways, depending on the configuration selected (e.g. Debug and Release).  In a similar way, Build Platforms allow you to target different hardware platforms, saving a different set of project properties for each platform.

The predefined Build Platform choices represent different hardware platforms that you might want to build your project for.  Each choice indicates the platform that the application is intended to run on.

  • x86 – Intended to run as a 32-bit process
  • x64 – Intended to run as a 64-bit process
  • Itanium – Intended to run on an Itanium processor
  • Any CPU – Intended to run as 32-bit or 64-bit

You can actually specify various project properties for any combination of Build Configuration and Build Platform.  But the Build Platform typically dictates the value of the Platform target choice on the Build tab of the Project Properties window.

Follow

Get every new post delivered to your Inbox.

Join 37 other followers