函数的条件宏 #define - 导致:“function”重新定义警告

发布于 2024-10-15 08:50:59 字数 775 浏览 2 评论 0原文

我刚刚看到这个帖子,描述了如何添加条件宏: #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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就此别过 2024-10-22 08:51:00

我在此处找到了我的问题的答案。

结论:
不能在#define中包含#ifdef等...,因为每行只能有一个预处理指令。

因此,虽然我们可以用反斜杠“\”来换行,但这有助于编写可读的多行宏,
但预处理器会将其视为一行:

#define doSomething(){ #if TARGET_IPHONE_SIMULATOR ... #endif }

这会引发此错误:

error: '#' is not followed by a macro parameter.

这是有道理的,所以我将不得不重新考虑我的实现。

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:

#define doSomething(){ #if TARGET_IPHONE_SIMULATOR ... #endif }

Which throws this error:

error: '#' is not followed by a macro parameter.

That makes sense, so I will have to rethink my implementation.

尐籹人 2024-10-22 08:51:00

此外,这可能会解决您收到的错误:
### 在宏定义内有特殊用途。 # 用于用双引号将宏参数括起来。 ## 用于连接两个宏参数。示例:
#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 in OK.
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?

友欢 2024-10-22 08:51:00

你的想法有一个怪癖,那就是类比/延伸。 doSomething() 必须被视为类似函数的宏。因此它的定义是矛盾的。缩小并查看下图:

doSomething() {
#if TARGET_IPHONE_SIMULATOR
   // conditionally compiled code
#else
   // platform-specific code
#endif
}

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:

doSomething() {
#if TARGET_IPHONE_SIMULATOR
   // conditionally compiled code
#else
   // platform-specific code
#endif
}
凶凌 2024-10-22 08:51:00

需要考虑的一种选择是在宏中创建一个将在编译时解析的条件。请考虑以下情况:

如果我想根据“c”的值调用不同的函数作为预处理器操作,我可以定义一个宏来静态检查“c”的值。

#define AorB(c) ((c>0) ? (Do_A(c)) : (Do_B(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.

#define AorB(c) ((c>0) ? (Do_A(c)) : (Do_B(c)))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文