Hi, why doesn't this code work:

int main()
{
	cout << "Hi!\n";
	#ifdef DEBUG
		cout << "I'm in debug mode!\n";
	#endif
	
	return 0;
}

I am trying to make the message "I'm in debug mode!" only print when I am in debug mode for my IDE (Visual C++). But also, I want this to work with most compilers. Is there any way to do this?

#define DEBUG 1 // on
//#define DEBUG 1 // off

>why doesn't this code work
Did you define DEBUG?

It is compiler dependent. Some compilers might define DEBUG, others _DEBUG, and still others might not define it at all. IMHO the best way to do it is to define the macro yourself in command-line options, such as -DDEBUG.

>why doesn't this code work
Did you define DEBUG?

If I do

#define DEBUG

Then even if I am in release mode it will still display the message.

It is compiler dependent. Some compilers might define DEBUG, others _DEBUG, and still others might not define it at all. IMHO the best way to do it is to define the macro yourself in command-line options, such as -DDEBUG.

_DEBUG worked for me.
I don't know how to define it myself in the command-line options :( .

Is it possible I could do something like

#ifdef DEBUG
#define _DEBUG 1
#endif
#ifdef _DEBUG
#define DEBUG 1
#endif

to get a compiler-independent solution?

>>to get a compiler-independent solution?
No. If you are using command-line builds then add -DDEBUG in the makefile. If you using IDE, how to do it will depend on the IDE.

>>to get a compiler-independent solution?
No. If you are using command-line builds then add -DDEBUG in the makefile. If you using IDE, how to do it will depend on the IDE.

I'm using the IDE version of Visual C++ Express 2008.
But, for my purposes right now, just using _DEBUG is enough. Mainly because I'll (probably) only be using Visual C++ for debugging for a while.

Thanks so far for your help all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.