#if DEBUG vs. Conditional( DEBUG )
It really depends on what you're going for:
#if DEBUG
: The code in here won't even reach the IL on release.[Conditional("DEBUG")]
: This code will reach the IL, however calls to the method will be omitted unless DEBUG is set when the caller is compiled.
Personally I use both depending on the situation:
Conditional("DEBUG") Example: I use this so that I don't have to go back and edit my code later during release, but during debugging I want to be sure I didn't make any typos. This function checks that I type a property name correctly when trying to use it in my INotifyPropertyChanged stuff.
[Conditional("DEBUG")]
[DebuggerStepThrough]
protected void VerifyPropertyName(String propertyName)
{
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
Debug.Fail(String.Format("Invalid property name. Type: {0}, Name: {1}",
GetType(), propertyName));
}
You really don't want to create a function using #if DEBUG
unless you are willing to wrap every call to that function with the same #if DEBUG
:
#if DEBUG
public void DoSomething() { }
#endif
public void Foo()
{
#if DEBUG
DoSomething(); //This works, but looks FUGLY
#endif
}
versus:
[Conditional("DEBUG")]
public void DoSomething() { }
public void Foo()
{
DoSomething(); //Code compiles and is cleaner, DoSomething always
//exists, however this is only called during DEBUG.
}
#if DEBUG example: I use this when trying to setup different bindings for WCF communication.
#if DEBUG
public const String ENDPOINT = "Localhost";
#else
public const String ENDPOINT = "BasicHttpBinding";
#endif
In the first example, the code all exists, but is just ignored unless DEBUG is on. In the second example, the const ENDPOINT is set to "Localhost" or "BasicHttpBinding" depending on if DEBUG is set or not.
Update: I am updating this answer to clarify an important and tricky point. If you choose to use the ConditionalAttribute
, keep in mind that calls are omitted during compilation, and not runtime. That is:
MyLibrary.dll
[Conditional("DEBUG")]
public void A()
{
Console.WriteLine("A");
B();
}
[Conditional("DEBUG")]
public void B()
{
Console.WriteLine("B");
}
When the library is compiled against release mode (i.e. no DEBUG symbol), it will forever have the call to B()
from within A()
omitted, even if a call to A()
is included because DEBUG is defined in the calling assembly.
C# if/then directives for debug vs release
DEBUG
/_DEBUG
should be defined in VS already.
Remove the #define DEBUG
in your code. Set preprocessors in the build configuration for that specific build.
The reason it prints "Mode=Debug" is because of your #define
and then skips the elif
.
The right way to check is:
#if DEBUG
Console.WriteLine("Mode=Debug");
#else
Console.WriteLine("Mode=Release");
#endif
Don't check for RELEASE
.
Conditional( Debug ) + #if DEBUG
Coincidentally I just happened to answer your question on my blog last week.
http://ericlippert.com/2009/09/10/whats-the-difference-between-conditional-compilation-and-the-conditional-attribute/
The #if directive is necessary if the body of the method refers to entities which are themselves declared under #if DEBUG directives. For example
#if DEBUG
static private int testCounter = 1;
#endif
[Conditional("DEBUG")] void CheckConsistency()
{
#if DEBUG
testCounter++;
#endif
...
That would not compile in the release build if you omitted the #if DEBUG in the method body.
#if DEBUG and Conditional(“DEBUG”) called in Release build
Something that may be obvious for some: the Build configuration changes based on the selected Configuration (eg. Debug) and Platform (eg. Any CPU).
In order to have #if DEBUG and Conditional("DEBUG") working as expected, the DEBUG constant must be defined in the 'Debug' configuration only!
Notice the difference:
Debug:
Release:
If DEBUG is defined in Release, when using #if DEBUG or Conditional("DEBUG"), the condition will return true because the DEBUG constant exists in the project configuration.
Yocto conditional debug flag addition
Currently you set
DEBUG_MODE = "1"
but then check for DEV_MODE
in your if condition, so this approach should not work at all.
Suggestion: Set -g
globally
One step could be to define TARGET_CPPFLAGS
in a *.conf
for your debug-image to include -g
.
TARGET_CPPFLAGS += "-g"
That way there should be no need to add it on recipe base, but can be done globally. Also when it is added in a conf only used for your debug image, there is no need for any if/else.
I would also use TARGET_CPPFLAGS, as those are then used for c and c++. But bear in min that some recipes might unset the compiler flags again.
Is it possible to use conditional compilation symbols in VS build events?
I finally found an answer. The following works perfectly:
if "$(DefineConstants.Contains('DEBUG'))" == "True" <command>
This works for any constants defined in the build, but note that the constant is case-sensitive ('DEBUG' != 'Debug').
#define DEBUG 1
When compiling, you should be able to specify an option to your compiler. For example, you can call GCC with the -DDEBUG
option.
In this case, you would be better using:
#ifdef DEBUG
#endif
or:
#if defined(DEBUG)
#endif
if this is not the way you're doing it now. I'm surprised that you don't have a global header file for your project. Something along the lines of:
#undef DEBUG
#define DEBUG 1
in a file called "debug.h". In your C programs, you can include this by using #include "debug.h"
Conditional installation with Wix
Use the preprocessor, e.g.: <?if?>
to conditionally include/exclude components based on configuration.
C# Conditional equivalent of !DEBUG
Using the attribute will be a bit of a hack but it can be done.
#if DEBUG
//you have nothing to do here but c# requires it
#else
#define NOT_DEBUG //define a symbol specifying a non debug environment
#endif
[Conditional("NOT_DEBUG")]
internal static void EncryptAppSettings()
{
//...
}
Related Topics
How to Run C# Project Under Linux
Capture Stored Procedure Print Output in .Net
Reading Email Using Pop3 in C#
Keeping ASP.NET Session Open/Alive
Ignoring a Class Property in Entity Framework 4.1 Code First
Efficient Way to Remove All Whitespace from String
Using Stored Procedure Output Parameters in C#
Update .Net Web Service to Use Tls 1.2
Wpf Createbitmapsourcefromhbitmap() Memory Leak
How to Get a List of Users from Active Directory
.Net Core MAChine Key Alternative for Webfarm
Casting Interfaces for Deserialization in JSON.Net
Get a Screenshot of a Specific Application
"The Controls Collection Cannot Be Modified Because the Control Contains Code Blocks"
How to Move Mouse Cursor Using C#
How Would You Do a "Not In" Query with Linq
How to Create a Dynamic Button Click Event on a Dynamic Button