开发者

How do I create my own defined constants based on the "Configuration Manager"?

开发者 https://www.devze.com 2023-01-21 09:27 出处:网络
When I select the \"Debug\" configuration, the DEBUG constant is active. When I select the \"Release\" configuration, the DEBUG constant is inactive.

When I select the "Debug" configuration, the DEBUG constant is active. When I select the "Release" configuration, the DEBUG constant is inactive.

How can I create my own configurations so that they include my ow开发者_如何学运维n defined constants. Basically, I want it so that if I select the configuration "FOOBAR" that there is a constant FOO and BAR in my project active.

I'm basically trying to avoid putting in a bunch of #define FOO in my projects, then commenting/uncommenting them out when I need/don't need them.


According to this article you can define compilation constants in the build tab of your project properties.

How do I create my own defined constants based on the "Configuration Manager"?

EDITED: To define build configurations you can go to Build > Configuration manager and I think you can define compilation constants there too.


Below I summarized how to create a new build configuration (say TEST_BUILD) other than debug/release build and how to define a conditional compilation under a new build configuration?

Go to Build -> Configuration Manager -> select "New" -> type TEST_BUILD and select the copy settings from -> Empty -> press OK.

  • Here you have to manually configure all the new settings for each and every projects under your solution file.
  • Alternatively you can copy either a debug or release build settings to your new TEST_BUILD apart from TEST_BUILD specific build settings.

    Step 1: Copy the existing settings to your new build config:
    copy setting from -> -> press OK. [or]
    copy setting from -> -> press OK.

    Step 2: Define a Macro:

    Goto Properties -> Configuration Properties -> C/C++ -> Preprocessor ->Preprocessor Definitions -> Define/Add TEST_BUILD

    Step 3: Enable/Disable the part of code using the macro TEST_BUILD


This is a very old question, but here's how you'd do it in VS2013.

Adding a constant in the Project Properties doesn't work for me.

How do I create my own defined constants based on the "Configuration Manager"?

What does work is to open the .csproj file for your project, find your configuration name, and add something to the "DefineConstants" section.

  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'BO4_Production|AnyCPU'">
    <OutputPath>bin\BO4_Production\</OutputPath>
    <DefineConstants>TRACE;BUSINESS_OBJECTS_4</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
    <Prefer32Bit>false</Prefer32Bit>
  </PropertyGroup>

In this example, whenever I'm in VS2013 and I change my Configuration to "BO4_Production", the source code will immediately reflect this, and any sections will reflect this:

#if BUSINESS_OBJECTS_4
    //  This is the URL of the Business Objects 4 REST services
    string BaseURL = "http://BO3Server:6405/biprws/logon/long");    
#else
    //  This is the URL of the Business Objects 3 web services
    string BaseURL = "http://BO3Server:8080/dswsbobje/services/Session";
#endif

It's strange that this seems to be the only way to make a #define kick in, just by changing the configuration.

A few months later...

Actually, scrap that. Even in VS2015, I can add my conditional symbol in either the "Build" tab or directly in the .csproj file, but some of the projects in my solution just get it wrong. For example, they have my symbol defined, when for that configuration, it shouldn't be defined. (I checked the Configuration Manager window, and it is all setup correctly, but VS2015 just doesn't get it right sometimes..)

0

精彩评论

暂无评论...
验证码 换一张
取 消