嵌套和未命名的联合与结构无法在gcc-4.6中进行编译. [英] Nested and unnamed unions and structures do not compile in gcc-4.6.

查看:131
本文介绍了嵌套和未命名的联合与结构无法在gcc-4.6中进行编译.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码在gcc 4.4下使用以下命令进行编译

gcc -fms-extensions实验.c

在gcc 4.6下出现错误

The code compiles under gcc 4.4 with the following command

gcc -fms-extensions experiment.c

Under gcc 4.6 I get errors

#include <stdio.h>

typedef unsigned char          uint8_t;
typedef unsigned short         uint16_t;

typedef signed char            sint8_t;
typedef signed short           sint16_t;

/*
** Decomposition of 16 bis
** in 8 bits
*/
typedef union raw_16_bits_entry
{
   uint16_t raw;
   sint16_t sraw;
   uint8_t  byte[2];

   /* H/L byte access */
   struct
   {
      uint8_t blow;
      uint8_t bhigh;

   } __attribute__((packed));

} __attribute__((packed)) raw16_t;

typedef union raw_msr_entry
{
   struct
   {
      uint8_t  eax;
      uint8_t  edx;

   } __attribute__((packed));

   raw16_t;

} __attribute__((packed)) msr_t;



typedef union amd_syscfg_msr
{
   struct
   {
      uint16_t   rsv0:4;
      uint16_t   mfde:1;
      uint16_t   mfdm:1;
      uint16_t   mvdm:1;
      uint16_t   tom2:1;

   } __attribute__((packed));

   msr_t;
   raw16_t;

} __attribute__((packed)) amd_syscfg_msr_t;

int main()
{

    amd_syscfg_msr_t q;
    q.rsv0=5;
    q.raw=7;
    printf("hello world!");
};




在gcc 4.6下,-fms-extensions出现错误




Under gcc 4.6 I get errors with -fms-extensions

exp1.c:15:13: error: duplicate member ‘raw’
exp1.c:16:13: error: duplicate member ‘sraw’
exp1.c:17:13: error: duplicate member ‘byte’
exp1.c:22:15: error: duplicate member ‘blow’
exp1.c:23:15: error: duplicate member ‘bhigh’
exp1.c: In function ‘main’:
exp1.c:66:6: error: ‘amd_syscfg_msr_t’ has no member named ‘raw’



没有-fms-extensions



without -fms-extensions

exp1.c:38:11: warning: declaration does not declare anything [enabled by default]
exp1.c:56:9: warning: declaration does not declare anything [enabled by default]
exp1.c:57:11: warning: declaration does not declare anything [enabled by default]
exp1.c: In function ‘main’:
exp1.c:66:6: error: ‘amd_syscfg_msr_t’ has no member named ‘raw’



我找不到任何有用的编译器选项.



I can not find any compiler options that will help.

推荐答案

您需要为所有联合成员指定名称.
在您的代码中,子工会没有名称->没有声明警告.
通过扩展,匿名子工会似乎将其成员直接介绍给当前的工会.这导致两次将原始成员等声明为两次.
您的工会组成将第一个工会映射了两次,看起来有点奇怪...

给所有成员起一个名字,所有成员都符合C语言.

干杯
安迪
You need to give names to all union members.
In your code, the sub-unions have no name -> no declaration warning.
With the extension, the anonymous sub-unions seem to introduce their members directly into the current union. This results in having the members raw, etc. declared twice.
Your composition of unions maps the first union twice, which looks a bit odd...

Give a name to all members, and all is C-language conformant.

Cheers
Andi


这篇关于嵌套和未命名的联合与结构无法在gcc-4.6中进行编译.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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