为什么C99会抱怨存储大小? [英] Why does C99 complain about storage sizes?

查看:738
本文介绍了为什么C99会抱怨存储大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在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


  1. net / if.h 包括 features.h

  2. 稍后,它在 #ifdef __USE_MISC 块中定义 struct ifreq

因此:


  1. 什么是 __ USE_MISC ? - 它是BSD和System V常见的东西

  2. 是否在此处定义? - 我们需要检查 features.h

features.h


  1. 当您使用 - -std = c99 默认情况下,GCC定义了 __ STRICT_ANSI __ (因为那就是C99)
  2. 预处理 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:

    1. net/if.h includes features.h
    2. Later on, it defines struct ifreq inside a #ifdef __USE_MISC block.

    So:

    1. What is __USE_MISC? -- it is stuff common to BSD and System V
    2. Is it defined at this point? -- We need to check that out in features.h

    So now, features.h:

    1. When you use --std=c99 GCC by default defines __STRICT_ANSI__ (since thats what C99 is)
    2. 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 of vimdiff.

    这篇关于为什么C99会抱怨存储大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆