Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging comments and C/C++ macros

I'm using g++ compiler, and I want certain lines of my c++ code to be commented out or not commented, depending on my config.

I realise that I could do:

#ifdef DEBUG
cout << "foo" << endl;
#endif

But I would rather it all be on a single line:

#define DEBUG //
DEBUG cout << "foo" << endl;

...with DEBUG being a macro for //. But writing #define DEBUG // yields nothing. Can anyone tell me what to do?

like image 610
JellicleCat Avatar asked Sep 18 '25 23:09

JellicleCat


1 Answers

But I would rather it all be on a single line:
#define DEBUG //

People have given good examples of how to accomplish what you want, but no one has commented on why your approach didn't work.

Your approach will never work. It can't work. There is no mechanism for defining a macro that becomes a start of comment sequence for the simple reason that comments don't exist by the time preprocessor symbols are defined. They have already been stripped out.

like image 129
David Hammen Avatar answered Sep 21 '25 13:09

David Hammen