2014-11-08 92 views
2

我试图从其他地方(特别是,here)得到一些代码,当gcc被赋予-pedantic标志时,编译时没有任何警告。唯一的问题是这一点的代码:gcc:fixed -pedantic“unnamed structure”warning

struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { 
    struct nlmsghdr nl_hdr; 
    /* Unnamed struct start. */ 
    struct __attribute__ ((__packed__)) { 
     struct cn_msg cn_msg; 
     struct proc_event proc_ev; 
    }; 
    /* Unnamed struct end. */ 
} nlcn_msg; 

无论我试图在结构的名称,它会导致编译错误。有没有办法修改给定的代码来满足-pedantic?或者有什么方法可以告诉gcc不要为那段代码发出警告?

+0

在看到这个[对SO帖子的回答](http://stackoverflow.com/a/133521/434551)。 '#pragma warning(disable:4068)'。也许在函数可能工作之前类似的东西。 – 2014-11-08 06:08:02

+0

'#pragma warning(disable:4068)'似乎不适用于gcc – 2014-11-08 06:34:29

+0

不同的警告号码,代表您试图阻止显示的警告,应该可以工作。 – 2014-11-08 06:36:08

回答

1

你正在编译哪个标准?

鉴于此代码:

#define NLMSG_ALIGNTO 4 

struct nlmsghdr { int x; }; 
struct cn_msg { int x; }; 
struct proc_event { int x; }; 

struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { 
    struct nlmsghdr nl_hdr; 
    /* Unnamed struct start. */ 
    struct __attribute__ ((__packed__)) { 
     struct cn_msg cn_msg; 
     struct proc_event proc_ev; 
    }; 
    /* Unnamed struct end. */ 
} nlcn_msg; 

与C99模式下编译,我得到的错误:

$ gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \ 
    -Wold-style-definition -Werror -pedantic -c x2.c 
x2.c:13:6: error: ISO C99 doesn’t support unnamed structs/unions [-Werror=pedantic] 
    }; 
    ^
cc1: all warnings being treated as errors 
$ 

与C11模式下编译,我没有得到任何错误:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \ 
     -Wold-style-definition -Werror -pedantic -c x2.c 
$ 
+0

使用'-std = c11'修复了我写的问题,但是介绍了它现在给出'隐式声明函数'siginterrupt''的问题,即使我包含'signal.h'。 – 2014-11-08 06:31:03

+0

然后将其更改为“-std = gnu11”......这允许在“c11”变体没有的时候创建大多数其他声明。 – 2014-11-08 06:32:40

+0

这样做,谢谢。 – 2014-11-08 06:35:37