Using preprocessor directives in Visual Studio 2010 using C #

I have a C # application that I create using Visual Studio 2010. To help me with some of my usual tasks in the application, I wanted to set some values ​​if I compiled the application in debug mode. Naturally, I would recommend preprocessor directives. My problem is that I do not quite understand how to use them. At the moment, I have a block of code that looks like this:

#define DEBUG ... // Other code in my app #if DEBUG myVariable = debugValue; #endif 

My problem is that when I compile my application in release mode, myVariable is still set to debugValue. It is like I'm not defining my preprocessor variable correctly, or I'm not setting my compiler correctly. Can someone explain to me what I need to do so that myVariable is set to debugValue when I compile the application in debug mode?

Thanks!

+6
source share
4 answers

If you use #define DEBUG to specify a debug symbol, switching to release mode will still contain the symbol, since you explicitly define it.

Try removing the #define DEBUG line in your code files. By default, VS defines DEBUG and TRACE in debug mode and TRACE in release mode, so you do not need to explicitly define them.

+11
source

If you are not aware of this, you can check the "Conditional" attribute. This allows you to decorate a method rather than specify preprocessor directives:

 class SomeClass { public void ProductionOperation() { //Doin' production stuff Log(someProductionVariable); } [Conditional("DEBUG")] public static void Log(string message) { //Write to a file } } 

If you compile in debug mode, the log method will write to the file. If you compile in release mode, the conditional method becomes no-op. The only thing to keep in mind here is that the conditional code will bring it into your assembly, unlike the way you outperformed it with the preprocessor - this is the difference in execution time. However, until you mind, I find this code cleaner.

(If you are going to do this, you do not want the #defining or #undefining DEBUG variable anywhere in your code).

+9
source

The DEBUG constant is actually defined in your project properties. Go to "Project Properties" β†’ "Insert Tab" β†’ "Define DEBUG Constant".

By explicitly declaring this constant, you override it declared by VS.

+5
source

Just add a little to answer Kyle and Steve.

If you open the .csproj file in a text editor, you can see how the Debug and Release configuration defines the characters.

 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> ... <DefineConstants>DEBUG;TRACE</DefineConstants> ... </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> ... <DefineConstants>TRACE</DefineConstants> ... </PropertyGroup> 

Active configuration is set using

 <Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration> 

but, as Kyle already mentioned, your code essentially adds it to all configurations.

+4
source

Source: https://habr.com/ru/post/898705/


All Articles