如何根据“配置管理器”创建自己定义的常量?

发布于 2024-09-28 01:13:11 字数 285 浏览 4 评论 0原文

当我选择“调试”配置时,DEBUG 常量处于活动状态。当我选择“Release”配置时,DEBUG 常量处于非活动状态。

我如何创建自己的配置,以便它们包含我自己定义的常量。基本上,我希望这样,如果我选择配置“FOOBAR”,则我的项目中有一个恒定的 FOOBAR 处于活动状态。

我基本上试图避免在我的项目中放入一堆#define FOO,然后在我需要/不需要它们时注释/取消注释它们。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执手闯天涯 2024-10-05 01:13:11

根据这篇文章你可以在项目属性的构建选项卡中定义编译常量。

在此处输入图像描述

已编辑: 要定义构建配置,您可以转到“构建”>“配置管理器,我认为你也可以在那里定义编译常量。

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

enter image description here

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

剧终人散尽 2024-10-05 01:13:11

下面我总结了如何创建新的构建配置(例如TEST_BUILD除了调试/发布构建以及如何在新的构建配置下定义条件编译

转到构建 ->配置管理器->选择“新建”->类型 TEST_BUILD
并从 -> 选择复印设置空->按确定。

  • 在这里,您必须为解决方案文件下的每个项目手动配置所有新设置。
  • 或者,除了 TEST_BUILD 特定的构建设置之外,您还可以将调试或发布构建设置复制到新的 TEST_BUILD。

    第 1 步:将现有设置复制到新的构建配置:
    复制设置从 -> ->按确定。 [或]
    复制设置从 -> ->按“确定”。

    第 2 步:定义宏:

    转到属性 ->配置属性-> C/C++->预处理器 -> 预处理器定义 ->定义/添加TEST_BUILD

    第 3 步:使用宏 TEST_BUILD 启用/禁用部分代码

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

月下客 2024-10-05 01:13:11

这是一个非常古老的问题,但在 VS2013 中你可以这样做。

在项目属性中添加常量对我来说不起作用

输入图片此处的描述

有效的方法是打开项目的 .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>

在这个例子中,每当我在 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

奇怪的是,这似乎是制作 #define 只需更改配置即可启动。

几个月后...

实际上,放弃吧。即使在 VS2015 中,我也可以在“Build”选项卡中添加条件符号,或者直接在 .csproj 文件中添加条件符号,但我的解决方案中的某些项目只是出错了。 >。例如,他们定义了我的符号,但对于该配置,不应定义它。 (我检查了配置管理器窗口,所有设置都正确,但 VS2015 有时无法正确设置..)

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.

enter image description here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文