visual-studio 如何根据“配置管理器”创建自己定义的常量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3981170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create my own defined constants based on the "Configuration Manager"?
提问by m-y
When I select the "Debug" configuration, the DEBUGconstant is active. When I select the "Release" configuration, the DEBUGconstant is inactive.
当我选择“调试”配置时,DEBUG常量处于活动状态。当我选择“发布”配置时,DEBUG常量处于非活动状态。
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 FOOand BARin my project active.
我如何创建自己的配置,以便它们包含我自己定义的常量。基本上,我想要它,如果我选择配置“FOOBAR”,那么我的项目中有一个常量FOO并且BAR处于活动状态。
I'm basically trying to avoid putting in a bunch of #define FOOin my projects, then commenting/uncommenting them out when I need/don't need them.
我基本上是想避免#define FOO在我的项目中放入一堆,然后在我需要/不需要它们时注释/取消注释它们。
回答by G?T?
According to this articleyou 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.
编辑:要定义构建配置,您可以转到构建 > 配置管理器,我认为您也可以在那里定义编译常量。
回答by SridharKritha
Below I summarized how to createa new build configuration(say TEST_BUILD) other than debug/release buildand how to define a conditional compilationunder a new build configuration?
下面我总结了如何创建除调试/发布构建之外的新构建配置(比如TEST_BUILD)以及如何在新构建配置下定义条件编译?
Go to Build -> Configuration Manager -> select "New" -> type TEST_BUILD and select the copy settings from -> Empty -> press OK.
转到构建 -> 配置管理器 -> 选择“新建” -> 键入 TEST_BUILD 并从 -> 空 -> 按确定选择复制设置。
- 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
- 在这里,您必须为解决方案文件下的每个项目手动配置所有新设置。
或者,除了特定于 TEST_BUILD 的构建设置之外,您还可以将调试或发布构建设置复制到新的 TEST_BUILD 中。
步骤 1:将现有设置复制到新的构建配置:
从 -> -> 复制设置 -> 按 OK。[或]
复制设置 -> -> 按 OK。第 2 步:定义一个宏:
转到属性 -> 配置属性 -> C/C++ -> 预处理器 -> 预处理器定义 -> 定义/添加 TEST_BUILD
第 3 步:使用宏 TEST_BUILD 启用/禁用部分代码
回答by Mike Gledhill
This is a very old question, but here's how you'd do it in VS2013.
这是一个非常古老的问题,但这是您在 VS2013 中的方法。
Adding a constant in the Project Properties doesn'twork 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.
有效的是打开项目的 .csproj 文件,找到配置名称,然后在“DefineConstants”部分添加一些内容。
<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:
在这个例子中,每当我在 VS2013 中并将我的配置更改为“BO4_Production”时,源代码将立即反映这一点,任何部分都会反映这一点:
#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 #definekick in, just by changing the configuration.
奇怪的是,这似乎是唯一#define可以通过更改配置来启动的方法。
A few months later...
几个月后...
Actually, scrap that. Even in VS2015, I can add my conditional symbol in eitherthe "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..)
其实,废了。即使在 VS2015 中,我也可以在“构建”选项卡中或直接在 .csproj 文件中添加我的条件符号,但我的解决方案中的某些项目只是出错了。例如,他们定义了我的符号,对于该配置,不应该定义它。(我检查了配置管理器窗口,所有设置都正确,但 VS2015 有时不正确..)

