警告...已定义但未使用 - 我真的需要 .c 文件吗?
我发现 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
这是真的,但我想知道以下问题的答案:
他们真的需要吗在某个库中定义了此符号(以消除此警告)?如果函数 OSAtomicDecrement32 只能通过调用 OSAtomicAdd32 创建,为什么不能直接在标头中定义它?他们应该使用#define OSAtomicDecrement32 ...body.. 来代替吗?
如果我不在程序中调用 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:
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?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜想 gcc 抱怨这一点的原因是 static 关键字,它导致该函数的代码在任何编译单元中发出。他们只是不应该这样做。他们要么使用
inline
而无需static
并仅在一个库(libc)中提供符号,例如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 useinline
withoutstatic
and provide the symbol in just one library, libc e.g.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.警告消息并没有说
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.