我刚刚看到这个帖子,描述了如何添加条件宏: Conditional value for a #define
但就我而言,我在条件内定义一个函数。
#if TARGET_IPHONE_SIMULATOR
#define doSomething(){\
\\ does something
}\
#else
#define doSomething(){\
\\ does something else
}\
#endif
这确实有效,除了我导致 gcc 编译器抛出此警告:
"doSomething" redefined
This is the location of the previous arguments
是否有任何解决方法可以帮助消除警告?
更新:
所以我尝试将条件包含在我的定义中:
#define doSomething(){\
#if TARGET_IPHONE_SIMULATOR
\\ do something
#else
\\ do something else
#endif
}\
但这会引发错误:
error: '#' is not followed by a macro parameter.
最佳答案
我找到了问题的答案 here .
结论: 你不能在#define中包含#ifdef等...,因为每行只能有一个预处理指令。
因此,虽然我们可以用反斜杠“\”来换行,但这有助于编写可读的多行宏, 但预处理器会将其视为一行:
#define doSomething(){ #if TARGET_IPHONE_SIMULATOR ... #endif }
这会引发此错误:
error: '#' is not followed by a macro parameter.
这是有道理的,所以我将不得不重新考虑我的实现。
关于gcc - 函数的条件宏 #define - 导致 : "function" redefined warning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4869440/