警告...已定义但未使用 - 我真的需要 .c 文件吗?

发布于 2024-12-15 08:17:51 字数 664 浏览 2 评论 0原文

我发现 Apple 在 OSAtomic.h 标头中使用以下声明:

inline static int32_t   OSAtomicDecrement32( volatile int32_t *__theValue )
            { return OSAtomicAdd32( -1, __theValue); }

在包含它并编译时,我收到以下警告:

warning: 'int32_t OSAtomicDecrement32(volatile int32_t*)' defined but not used

这是真的,但我想知道以下问题的答案:

  1. 他们真的需要吗在某个库中定义了此符号(以消除此警告)?如果函数 OSAtomicDecrement32 只能通过调用 OSAtomicAdd32 创建,为什么不能直接在标头中定义它?他们应该使用#define OSAtomicDecrement32 ...body.. 来代替吗?

  2. 如果我不在程序中调用 OSAtomicDecrement32,为什么它会抱怨符号 OSAtomicAdd32 未定义(当我不链接到定义它的库时)?它是内联的,我认为当我不使用某些东西时,编译器会将其剥离...

感谢您向我解释!

I find out that Apple uses the following declaration in OSAtomic.h header:

inline static int32_t   OSAtomicDecrement32( volatile int32_t *__theValue )
            { return OSAtomicAdd32( -1, __theValue); }

I am getting the following warning when including it and compiling:

warning: 'int32_t OSAtomicDecrement32(volatile int32_t*)' defined but not used

That is true, but I would like to know answers to the following:

  1. Do they really need to have this symbol defined in some library (to get rid of this warning)? If the function OSAtomicDecrement32 can be created only by calling OSAtomicAdd32, why one can't define this in a header directly? Should they use #define OSAtomicDecrement32 ...body.. instead?

  2. If I don't call OSAtomicDecrement32 in my program, why does it complain that symbol OSAtomicAdd32 is undefined (when I don't link with a library which defines it)? It is inline, I thought that when I don't use something, the compiler would strip it off...

Thanks for explaining it to me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

吐个泡泡 2024-12-22 08:17:51

我猜想 gcc 抱怨这一点的原因是 static 关键字,它导致该函数的代码在任何编译单元中发出。他们只是不应该这样做。他们要么使用

  • 清晰的 C99 标准代码。然后他们只需要做inline而无需
    static 并仅在一个库(libc)中提供符号,例如
  • 使用 gcc 的(以及 clang 的)属性等编译器扩展
    always_inline

但绝不static

顺便说一句,gcc对此操作有扩展(以__sync IIRC为前缀),可以移植到其他有gcc、clang、icc、opencc的系统......

也许你可以通过添加一些来解决这个问题gcc 调用的参数。尝试 -std=c99 或关闭相应的警告。

I guess the reason for gcc complaining about this is the static keyword which results in code for this function to be emitted in any compilation unit. They just shouldn't do it like this. Either they'd use

  • clear C99 standard code. Then they'd just have to do inline without
    static and provide the symbol in just one library, libc e.g.
  • use compiler extensions like gcc's (and thus clang's) attribute
    always_inline

but never static.

BTW, gcc has extension for this operations (prefixed with __sync IIRC) that would be portable to other systems where there is gcc, clang, icc, opencc...

Perhaps you could get around this by adding some arguments to the gcc call. Try -std=c99 or switch off the corresponding warning.

○愚か者の日 2024-12-22 08:17:51

警告消息并没有说 OSAtomicDecrement32 未定义,恰恰相反。该消息告诉您有一个已定义的函数,但您没有调用它。如果它来自外部库,则无需担心,如果它在您自己的代码中,那么您可能只是还没有编写调用它的代码。

The warning message doesn't say that OSAtomicDecrement32 is undefined, quite the opposite. The message is telling you that there is a function that is defined but you do not call it. If it's from an external library there is nothing to worry about, and if it's in your own code then maybe you just haven't written the code to call it yet.

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