Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why compiler complain about this macro declaration

I write the following macro for debug convinience,

1 #ifndef DEF_H
2 #define DEF_H
3 #define DEBUG_MODE
4 #define DEBUG_INFO(message)     \
5         #ifdef DEBUG_MODE       \
6                 cout << message << endl; \
7         #endif                          \
8 #endif

but gcc complains as the following

def.h:4: error: '#' is not followed by a macro parameter
def.h:1: error: unterminated #ifndef

What's wrong with this piece of code? Do I miss some important points here?

like image 216
speedmancs Avatar asked Apr 09 '12 14:04

speedmancs


Video Answer


1 Answers

You cannot have #ifdefs inside a macro definition. You need to turn it inside out:

#ifdef DEBUG_MODE
#define DEBUG_INFO(message) cout << (message) << endl
#else
#define DEBUG_INFO(message)
#endif
like image 151
Oliver Charlesworth Avatar answered Oct 12 '22 01:10

Oliver Charlesworth