如何根据“配置管理器”创建自己定义的常量?
当我选择“调试”配置时,DEBUG
常量处于活动状态。当我选择“Release”配置时,DEBUG
常量处于非活动状态。
我如何创建自己的配置,以便它们包含我自己定义的常量。基本上,我希望这样,如果我选择配置“FOOBAR”,则我的项目中有一个恒定的 FOO
和 BAR
处于活动状态。
我基本上试图避免在我的项目中放入一堆#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据这篇文章你可以在项目属性的构建选项卡中定义编译常量。
已编辑: 要定义构建配置,您可以转到“构建”>“配置管理器,我认为你也可以在那里定义编译常量。
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.
下面我总结了如何创建新的构建配置(例如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.
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
这是一个非常古老的问题,但在 VS2013 中你可以这样做。
在项目属性中添加常量对我来说不起作用。
有效的方法是打开项目的 .csproj 文件,找到配置名称,然后在“DefineConstants”部分添加一些内容。
在这个例子中,每当我在 VS2013 中并将配置更改为“BO4_Production”时,源代码将立即反映这一点,并且任何部分都会反映这一点:
奇怪的是,这似乎是制作
#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.
What does work is to open the .csproj file for your project, find your configuration name, and add something to the "DefineConstants" section.
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:
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..)