Bac*_*ach 3 macros gcc c-preprocessor
我刚看到这个帖子,描述了如何添加条件宏: #define的条件值
但在我的情况下,我在条件中定义一个函数.
#if TARGET_IPHONE_SIMULATOR
#define doSomething(){\
\\ does something
}\
#else
#define doSomething(){\
\\ does something else
}\
#endif
Run Code Online (Sandbox Code Playgroud)
这确实有效,除了我导致gcc编译器抛出此警告:
"doSomething" redefined
This is the location of the previous arguments
Run Code Online (Sandbox Code Playgroud)
是否有任何解决方法来帮助摆脱警告?
更新:
所以我尝试在我的定义中包含条件:
#define doSomething(){\
#if TARGET_IPHONE_SIMULATOR
\\ do something
#else
\\ do something else
#endif
}\
Run Code Online (Sandbox Code Playgroud)
但是会抛出一个错误:
error: '#' is not followed by a macro parameter.
Run Code Online (Sandbox Code Playgroud)
我在这里找到了我的问题的答案.
结论: 你不能在#define中加入#ifdef等...因为每行只应该有一个预处理指令.
因此,虽然我们可以用反斜杠'\'打破这一行,但这有助于编写可读的多行宏,但预处理器会将其视为一行:
#define doSomething(){ #if TARGET_IPHONE_SIMULATOR ... #endif }
Run Code Online (Sandbox Code Playgroud)
这引发了这个错误:
error: '#' is not followed by a macro parameter.
Run Code Online (Sandbox Code Playgroud)
这是有道理的,所以我将不得不重新考虑我的实施.