Finding Debug or Release mode in C++ through coding - c++

My application saves a few files in specified paths depending on which mode the program is running....that is either debug or release mode...How can I find out through coding that in which mode my program is currently running on? so that I can write the correct if else statement..
Please help me..
I am using VS2005 with C++98..

You can use the preprocessor symbols _DEBUG and NDEBUG:
#ifdef _DEBUG
std::cout << "in debug mode";
#else
std::cout << "in release mode";
#endif

Related

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;
}

How to compile so that filenames / line numbers are available for error-reporting tool?

I have a game that is sold on Steam, and as such uses the Steamworks SDK. This has an automatic error-collecting tool as described briefly here.
Every time my game generates an unhandled exception, it is logged on the tool's web site. I've noticed that when the crash occurs on MY development build, the logged crash includes filenames and line numbers. However, when the crash occurs on a user machine, this info is absent.
Is this probably because I have the PDBs on my machine but not the user's machine?
Are there any compilation flags that might bake limited information into the EXE, so that the error reporting tool might be able to grab it?
I realize this is a bit of a longshot question and asked in relation to a specific tool. I asked because I'm hoping there is general knowledge (about compilation flags, etc) which I can apply to my specific situation.
I don't know Steamworks SDK, but will at least try to explain common usage of preprocessor NDEBUG, _DEBUG, __FILE__ and __LINE__ on classic assert.h (taken from Windows SDK / VC include):
#include <crtdefs.h>
#undef assert
#ifdef NDEBUG
#define assert(_Expression) ((void)0)
#else
#ifdef __cplusplus
extern "C" {
#endif
_CRTIMP void __cdecl _wassert(_In_z_ const wchar_t * _Message, _In_z_ const wchar_t *_File, _In_ unsigned _Line);
#ifdef __cplusplus
}
#endif
#define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
#endif /* NDEBUG */
Release Build usually disables asserts by defining NDEBUG while Debug Build usually leave NDEBUG undefined (to enable asserts) and include _DEBUG for additional checks (while Work Build may have both undefined). Look at the definition of assert:
#define assert(_Expression) (void)( (!!(_Expression)) \
|| (_wassert(_CRT_WIDE(#_Expression), \
_CRT_WIDE(__FILE__), __LINE__), 0) )
If everything else fails (defining/undefining NDEBUG / _DEBUG) you can use __FILE__ and __LINE__ yourself - to include that in whatever message string you are passing to the engine (or to those exceptions you may throw).
I'm going to presume you export code in Release Mode in Visual Studio, as opposed to Debug.
Visual Studio removes (by optimizing) some debugging elements, such as Memory Logging (_CrtDumpMemoryLeaks), but I am not an expert in what it does and doesn't remove. I would start with the link below, which covers debugging in release mode.
http://msdn.microsoft.com/en-us/library/fsk896zz.aspx

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

#ifdef _DEBUG release mode in vs 2008

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.

How to use #ifdef _DEBUG on Linux?

I have a problem with _DEBUG macro on Linux C++.
I tried to use something like this:
#ifdef _DEBUG
cout << "Debug!" << endl;
#endif
But it doesn't work when I select Debug in IDE. However it worked on Windows.
I use Eclipse IDE for C++ coding on Linux.
On windows you were probably using Visual Studio, which automatically defines the _DEBUG symbol when building the solution in Debug mode. Symbols are usually defined when invoking the compiler, and then they will be used during the pre-processor phase.
For example, if you are using GCC via command line and you want to re-use the _DEBUG symbol because you are handling an already established codebase, you can define it using the -D parameter via command line.
#include <iostream>
int main() {
std::cout << "Hello world!\n";
#ifdef _DEBUG
std::cout << "But also Hello Stack Overflow!\n";
#endif
return 0;
}
Compiling this with the following command line
g++ -D _DEBUG define.cpp
will give the following result
Hello world!
But also Hello Stack Overflow!
Your best bet for eclipse cdt however could be to modify the paths and symbols properties in the project properties.
If you use cmake put the following in your your top-level CMakeLists.txt file
set_directory_properties(PROPERTIES COMPILE_DEFINITIONS_DEBUG "_DEBUG")
found on CMake Mailinglist
Put #define _DEBUG somewhere north of where you have this snippet, or in your project settings.