1. 程式人生 > >Xcode DEBUG 巨集定義報錯

Xcode DEBUG 巨集定義報錯

#define __DEBUG__
#ifdef __DEBUG__
#define DEBUG(format,...) printf("File: "__FILE__", Line: %05d: "format"/n", __LINE__, ##__VA_ARGS__)
#else
#define DEBUG(format,...)
#endif
以上在STM32 System Workbench 編譯通過,但是在Xcode 9.3 (Apple LLVM version 9.1.0 (clang-902.0.39.1))上編譯C程式會提示錯誤:

Invalid suffix on literal; C++11 requires a space between literal and identifier
Fix insert" "
No matching literal operator for call to 'operator""__FILE__' with arguments of types 'const char *' and 'unsigned long', and no matching literal operator template

需要在format前面和__File__前後都新增空格,修改成
#define __DEBUG__
#ifdef __DEBUG__
#define DEBUG(format,...) printf("File: " __FILE__ ", Line: %05d: " format "/n", __LINE__, ##__VA_ARGS__)
#else
#define DEBUG(format,...)
#endif

但是還是會有提示 
'DEBUG' macro redefined
把DEBUG替換成 Mylog 就好了
#define __Mylog__
#ifdef __Mylog__
#define Mylog(format,...) printf("File: " __FILE__ ", Line: %05d: " format"/n", __LINE__, ##__VA_ARGS__)
#else
#define Mylog(format,...)
#endif