为什么C99会抱怨存储大小? [英] Why does C99 complain about storage sizes?
问题描述
这是我在Linux上编译的一些代码:
#include< net / if.h>
int main(){
struct ifreq ifr;
}
gcc test.c
很好。
gcc -std = gnu99 test.c
很好。
gcc -std = c99 test.c
失败,并显示以下错误:
test.c:在函数'main'中:
test.c:4:16:error:'ifr'的存储大小未知
C99的不同之处在于它不喜欢 struct ifreq
在Linux?
这是预处理和GNU C与C99的后果链。 b $ b
首先, net / if.h
:
-
net / if.h
包括features.h
- 稍后,它在
#ifdef __USE_MISC
块中定义struct ifreq
。
因此:
- 什么是
__ USE_MISC
? - 它是BSD和System V常见的东西 - 是否在此处定义? - 我们需要检查
features.h
, features.h
:
- 当您使用
- -std = c99
默认情况下,GCC定义了__ STRICT_ANSI __
(因为那就是C99)
- 预处理
features.h
,当__ STRICT_ANSI __
开启时,BSD和System V功能不会启动,即<$ c
$ b备份到
net / if $ c $ _ $ US $ _ $ c $ _ $> $ / $> .h
:struct ifreq
在预处理后不存在! 因此,关于存储空间的抱怨。
您可以通过这样做来理解整个故事:
vimdiff <(cpp test.c --std = c99 -dD)<(cpp test.c --std = gnu99 -dD)
或以任何其他方式区分它们(例如
diff - side-by-side
)而不是
vimdiff
。This is some code I'm compiling on Linux:
#include <net/if.h> int main() { struct ifreq ifr; }
gcc test.c
is fine.gcc -std=gnu99 test.c
is fine.gcc -std=c99 test.c
fails with the following error:test.c: In function ‘main’: test.c:4:16: error: storage size of ‘ifr’ isn’t known
What's different about C99 that it doesn't like the definition of
struct ifreq
in Linux?解决方案It's a chain of consequences of preprocessing and GNU C vs C99.
First up,
net/if.h
:net/if.h
includesfeatures.h
- Later on, it defines
struct ifreq
inside a#ifdef __USE_MISC
block.
So:
- What is
__USE_MISC
? -- it is stuff common to BSD and System V - Is it defined at this point? -- We need to check that out in
features.h
So now,
features.h
:- When you use
--std=c99
GCC by default defines__STRICT_ANSI__
(since thats what C99 is) - While preprocessing
features.h
, when__STRICT_ANSI__
is on, the BSD and System V features don't kick in. i.e.__USE_MISC
is left undefined.
Back up to
net/if.h
:struct ifreq
does not even exist after preprocessing! Therefore, the complaint about storage size.You can catch the whole story by doing:
vimdiff <(cpp test.c --std=c99 -dD) <(cpp test.c --std=gnu99 -dD)
or diff'ing them in any other way (like
diff --side-by-side
) instead ofvimdiff
.这篇关于为什么C99会抱怨存储大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!