2

I am moving some C++ code to OS X (Maverick) which was previously compiled on Win VC++ 2012 and Linux GCC 4.7 .I have the following macro to print messages to console in debug mode:

#ifdef DEBUG
  #define  PrintDebug(msg) Msg::PrintMsg msg
#else
  #define  PrintDebug(msg) (void)0
#endif

It worked but in XCode the compiler(clang) spits errors like "expected ';' after expression" and "too many arguments provided to function like macro invocation"

Important to note,the input to the macro argument has the same format as

printf(fmt,...) 

For example using it with message only:

 PrintDebug("Some message\n");

throws compile time error: "expected ';' after expression"

And when I pass the formatting:

  PrintDebug("Number:%d\n",someNumber);

the error is :too many arguments provided to function like macro invocation

I tried several other variations from this SO thread but didn't find a match which would work with all 3 compilers. How do I get it working with CLANG but also be compatible with MSVC and GCC compilers?

Community
  • 1
  • 1
Michael IV
  • 11,016
  • 12
  • 92
  • 223

1 Answers1

0

You can use variadic macros for this:

#ifdef DEBUG
  #define  PrintDebug(fmt, args...) Msg::PrintMsg (fmt, args)
#else
  #define  PrintDebug(fmt, args...) (void)0
#endif
TartanLlama
  • 63,752
  • 13
  • 157
  • 193