函数的条件宏 #define - 导致:“function”重新定义警告
我刚刚看到这个帖子,描述了如何添加条件宏: #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.
I just saw this thread, describing how to add conditional macros:
Conditional value for a #define
but in my case I am defining a function within the condition.
#if TARGET_IPHONE_SIMULATOR
#define doSomething(){\
\\ does something
}\
#else
#define doSomething(){\
\\ does something else
}\
#endif
This does work, except I is causing gcc compiler to throw this warning:
"doSomething" redefined
This is the location of the previous arguments
Is there any workaround to help getting rid of the warnings?
UPDATE:
So I tried including the condition inside my definition:
#define doSomething(){\
#if TARGET_IPHONE_SIMULATOR
\\ do something
#else
\\ do something else
#endif
}\
but that throws an error:
error: '#' is not followed by a macro parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在此处找到了我的问题的答案。
结论:
你不能在#define中包含#ifdef等...,因为每行只能有一个预处理指令。
因此,虽然我们可以用反斜杠“\”来换行,但这有助于编写可读的多行宏,
但预处理器会将其视为一行:
这会引发此错误:
这是有道理的,所以我将不得不重新考虑我的实现。
I found the answer to my question here.
Conclusion:
you cannot include #ifdef etc... inside #define, because there should only be one pre-processing directive per line.
So although we can break the line with a backslash '\' this helps writing readable multiline macros,
but the preprocessor will see it as one line:
Which throws this error:
That makes sense, so I will have to rethink my implementation.
此外,这可能会解决您收到的错误:
#
和##
在宏定义内有特殊用途。#
用于用双引号将宏参数括起来。##
用于连接两个宏参数。示例:#define ABC(X) #X
ABC(hello)
结果为"hello"
。#define XYZ(X,Y) X##Y
XYZ(O,K)
结果为OK
。请注意,此功能(可能)是 ANSI C 独有的。
另外,为什么要使用这样的宏?某个功能可能更适合您吗?
Also, this might address the error you received:
#
and##
have special purposes inside macro definitions.#
is used to surround a macro parameter with double quotes.##
is used to concatenate two macro parameters. Example:#define ABC(X) #X
ABC(hello)
results in"hello"
.#define XYZ(X,Y) X##Y
XYZ(O,K)
results inOK
.Note that this feature is (possibly) unique to ANSI C.
Also, why would you be using a macro like this? Might a function work better for you?
你的想法有一个怪癖,那就是类比/延伸。 doSomething() 必须被视为类似函数的宏。因此它的定义是矛盾的。缩小并查看下图:
There is a quirk in your thinking which is by analogy/ extension. doSomething() has to be viewed as a function-like macro. As such its definition is ambivalent. Zoom out and see below:
需要考虑的一种选择是在宏中创建一个将在编译时解析的条件。请考虑以下情况:
如果我想根据“c”的值调用不同的函数作为预处理器操作,我可以定义一个宏来静态检查“c”的值。
然后,如果您配置了一个优化级别来删除永远无法访问的分支,那么它应该删除未执行的情况。这可能不正是您正在寻找的。
One option to consider is creating a condition in a macro that will resolve at compile time. Consider the following:
If I would like to call a different function based on the value of 'c' as a pre-processor action, I can define a macro that checks the value of 'c' statically.
Then if you configure a level of optimization that removes branches that are never reachable, it should strip out which ever case wasn't performed. This may not exactly be what you were looking for.