How do I create my own defined constants based on

2020-07-02 07:54发布

问题:

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 own 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.

回答1:

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

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



回答2:

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



回答3:

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.

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..)