I have DEBUG, RELEASE and MYMODE configuration modes. How can I detect them?
#if DEBUG
#endif
detect only DEBUG and anything else, but not difference between RELEASE and MYMODE. How can I do it?
MORE INFO:
I was thinking about something like this:
#if DEBUG
#elif RELEASE
#elif MYMODE
but if I have RELEASE or MYMODE, all parts of code are gray, only for DEBUG mode the DEBUG part is colorful. I also tried
#define MYMODE
but for this, MYMODE is true all the time doesn't metter what mode is set.
First you can use boolean operators in #if
:
#if DEBUG || MYMODE
…
#endif
Second you can use #else
and #elif
to break up things...
#if DEBUG
// Debug only
#elif MYMODE || RELEASE
// In either release or MYMODE
#else
// Otherwise
#endif
The combination of these two covers most use cases.
MyMode is a configuration . But, in and of itself, that doesn't define any conditional compilation symbols.
You change these through the projects compilation settings 1 or by passing the -define
option to csc. If you look through the Debug configuration's compilation settings, you'll find that the DEBUG
conditional compilation symbol was already defined 2 , but there's no RELEASE
symbol defined in the Release configuration.
There is no requirement (as you'll find above) that there be any relation between configurations and the symbols that they define.
#if
(and family) is defined to work with conditional compilation.
1 Project -> Properties -> Build -> General.
2 In some versions of Visual Studio, there's a dedicated checkbox for it rather than it being listed in the Conditional Compilation symbols, but the effect is the same. If you unload the project and examine the XML, you'll find that all constants are stored in the <DefineConstants>
element.
The same:
#if MYMODE
#endif
#if RELEASE
#endif
Or more complex :
#if (DEBUG && MYMODE)
#endif
#if (!RELEASE && MYMODE)
#endif
I found the solution also thanks by you guys. I used this code:
#if DEBUG
#elif MYMODE
#else
but first I needed to put the name MYMODE
to the Properties > Build > Conditional Compilation Symbols. I didn't do it before so there was the problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.