I have a C# application that I am building with Visual Studio 2010. To help me with some of my routine tasks in the app, I wanted to set some values if I compiled the app in debug mode. Naturally, I though preprocessor directives would be a good idea. My problem is, I don't quite understand how to use them. At this time, 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, when I compile my app in release mode, myVariable still gets set to debugValue. It's like I'm not properly defining my preprocessor variable or I'm not configuring my compiler correctly. Can anybody explain to me what I need to do so that myVariable is only set to debugValue when I compile the app in debug mode?
Thank you!
It is a pre-process of execution of a program using c/c++ language. To initialize a process of preprocessor commands, it's mandated to define with a hash symbol (#). It can preferably be the non-blank character, and for better readability, a preprocessor directive should start in the first column.
If you want to install the directive processor in the experimental version of Visual Studio, insert "Exp" after "11.0". Add a registry key that has the same name as the directive processor class. In the registry tree, right-click the DirectiveProcessors node, point to New, and then click Key.
If you are using #define DEBUG
to specify the debug symbol, switching to release mode will still provide the symbol, since you are explicitly defining 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 its not necessary to explicitly define them.
If you aren't aware of it, you might want to check out the "Conditional" attribute. This lets you decorate a method rather than inlining 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're compiling in debug mode, the log method will write to a file. If you're compiling in release mode, the conditional method becomes a no-op. Only thing to bear in mind here is that the conditional code will make it into your assembly, unlike if you preempt it with the preprocessor - this is a runtime distinction. However, as long as you don't mind that, I've found this keeps the code cleaner.
(If you're going to do this, you don't want to be #defining or #undefining the DEBUG variable anywhere in your code).
The DEBUG constant is actually defined in your project properties. Go to Project Properties->Build tab->Define DEBUG constant.
By explicitly declaring that constant, you are overriding the one declared by VS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With