#ifdef _DEBUG release mode in vs 2008 - c++

There are some part of my project which don't function in release mode. I can check it by using printf and it doesn't print anything. I'll show you in this following code:
void SNKsomething::vGetState()
{
#ifdef SNK_STH
for(int i = 0; i < 2; i++)
{
printf("sth\n');
}
Additionally, SNK_STH is defined in files Globals.h as following
#ifdef _DEBUG // in Project properties
#define SNK_STH
#else
// .....
So, I don't see sth which I print it in release mode. I want to know that I have to do something about _DEBUG in project properties of VS-2008. don't I?

_DEBUG is a preprocessor macro. If you right click on properties and go to c/c++, preprocessor is one of the options.
Preprocessor definitions are different for release and debug. If you add SNK_STH to the release preprocessor macros you will see your printf.
If you want to see the code in both debug and release, consider pulling it out of the ifdef.

I've had problems with the _DEBUG Macro, what I found very usefull is the
IsDebuggerPresent function
which returns a boolean:
If the current process is running in the context of a debugger, the return value is nonzero.
If the current process is not running in the context of a debugger, the return value is zero.

Related

Is debugging code not going to production in c++? [duplicate]

Does #ifdef _DEBUG in the main function has any sense if I am working on visual studio 2013 ?
If yes, what it is for ?
int _tmain(int argc, _TCHAR* argv[])
{
#ifdef _DEBUG
//creating some objects, using functions etc;
#endif
}
#ifdef DEBUG or #ifdef _DEBUG are used to handle some code that you use for debugging purposes. If you add #undef _DEBUG or similar to this at the very beginning of your code, the compiler will skip the code contained in #ifdef DEBUG /* bla bla */ #endif.
If you're
//creating some objects, using functions etc;
inside this block and thinking this will work, I assure you, it won't until you include -D_DEBUG in compiler's options during invocation.
for example it can be used to do stuff only in debug compilation ;)
Check your flags specifically to VS, in linux for example, you add NDEBUG to non debug builds.. so check your project properties.. compile command line.. etc..

How to view the output of DEBUG conditional statement?

I have written a code with #ifdef DEBUG conditional statement to print cout statement in code blocks. My questions are:
so these conditional Debug will appear only during condition right?
if so how do I view the output in code blocks when I debug the code?
I am not sure about codeblocks but in visual studio you can select if you want to build the debug or release version of the program (or any other version you define). What this effectively does is that it will set the flag DEBUG to true. and you don't need to define a variable manually. In any case you can use your own definition for this.
In the debug version anything inside the #ifdef DEBUG will be also compiled while in the release version these chunks of code will be skipped. To get information from debugging you can define a macro debug print like this.
#define DEBUG_MODE 1 // Or 0 if you dont want to debug
#ifdef DEBUG_MODE
#define Debug( x ) std::cout << x
#else
#define Debug( x )
#endif
and then call your Debug( someVariable ); If you build the debug version you get output in the console otherwise nothing will happen.
As mentioned in other comments/answers you can define a macro such as DEBUG(message) for printing debug messages in debug builds only. However, I suggest you use NDEBUG instead of DEBUG to do so. NDEBUG is a standardized predefined macro that is automatically defined by compiler in release build, if this is your intent. Use it this way :
// #define NDEBUG ==> not needed, this macro will be predefined by compiler in release build
#ifdef NDEBUG // release build
# define DEBUG(msg)
#else // debug build
# define DEBUG(msg) std::cout << msg
#endif
int main(void)
{
DEBUG("this will be printed to console in debug build only\n");
return 0;
}

#ifdef _DEBUG in main function

Does #ifdef _DEBUG in the main function has any sense if I am working on visual studio 2013 ?
If yes, what it is for ?
int _tmain(int argc, _TCHAR* argv[])
{
#ifdef _DEBUG
//creating some objects, using functions etc;
#endif
}
#ifdef DEBUG or #ifdef _DEBUG are used to handle some code that you use for debugging purposes. If you add #undef _DEBUG or similar to this at the very beginning of your code, the compiler will skip the code contained in #ifdef DEBUG /* bla bla */ #endif.
If you're
//creating some objects, using functions etc;
inside this block and thinking this will work, I assure you, it won't until you include -D_DEBUG in compiler's options during invocation.
for example it can be used to do stuff only in debug compilation ;)
Check your flags specifically to VS, in linux for example, you add NDEBUG to non debug builds.. so check your project properties.. compile command line.. etc..

Runtime how to determine Debug or release mode in visual studio c++

I am doing my school assignment. During debug mode I would like to turn on my console mode and during release turn off console.
I have try marco as recommended in stackoverflow but it is not working. I am using visual studio 2012 (empty project c++)
#if DEBUG
//doing something
#else
//Release mode doing something
#endif
#if DEBUG will only work if you define DEBUG via the compiler options.
By default, DEBUG is not defined, but _DEBUG is. Try #if defined(_DEBUG), or change your compiler options (via Project Properties / Configuration Properties / C/C++ / Preprocessor / Preprocessor Definitions) to define DEBUG.
#if DEBUG will resolve itself at compile time not at run time.
NDEBUG is pretty standard macro defined in release mode.
And I think Visual studio defines _DEBUG macro when in debug mode.
In any case you can define your own macros in Visual Studio
Go to Project->Properties->Configuration Properties->C/C++->Preprocessor -> Preprocessor Definitions There you can add macros for your project in the build configuration you have chosen.
From your comments it seems that tproblem you're running into is getting a console window open and connected to stdout (having little to do with DEBUG vs. RELEASE builds).
See the MS Support Article INFO: Calling CRT Output Routines from a GUI Application for a working example of how to have a GUI program open a console and direct stdout to it:
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
// ...
int hCrt;
FILE *hf;
AllocConsole();
hCrt = _open_osfhandle(
(long) GetStdHandle(STD_OUTPUT_HANDLE),
_O_TEXT
);
hf = _fdopen( hCrt, "w" );
*stdout = *hf;
int i = setvbuf( stdout, NULL, _IONBF, 0 );
puts("hello world");
Actually, on further testing, your simpler technique of using freopen("CONOUT$","w",stdout); works too. For some reason in my initial tests it didn't seem to work. You may need to also have the setvbuf() call to avoid buffering problems.
This is for Visual studio 2019:
#ifdef _DEBUG
// do something in a debug build
#else
// do something in a release build
#endif
For C# the constants DEBUG works fine, Just make sure that in the project properties it is enabled.
Go to project properties (by right clicking on your project in solution explorer), then select build option on right side of the window and check the define DEBUG constant checkbox.
Then you can use code like this.
#if DEBUG
// debug mode
#else
//release mode
#endif

Compiling with Ogre + MFC in _DEBUG mode

There is a problem compiling Ogre with MFC in debug mode, you get an error because of the MFC macro:
#ifdef _DEBUG
#define new DEBUG_NEW
Which basically clobbers Ogre's debug new -
#define OGRE_NEW new (__FILE__, __LINE__, __FUNCTION__)
I'm trying to get MFC+Ogre to run merrily together in DEBUG mode, and I got it to compile with:
#ifdef _DEBUG
#undef new
#endif
Ogre::Root * root = OGRE_NEW Ogre::Root( pluginsFile, "ogre.cfg", "Ogre.log" );
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
But now, I get a runtime error: Ogre::InternalErrorException
Anybody else face/solve this problem?
You may need to remove the MFC debug macro rather than the Ogre one. Check this article on the Ogre Wiki here titled Common Mistakes Ogre Wiki
I think this might be a problem in the specific machine I was using. I tried this out on another machine, and it seemed to work in debug mode with the #ifdefs #undefs as shown above.