2

我正在尝试从其他地方(特别是这里)获取一些代码,以便在 gcc 被赋予-pedantic标志时在没有任何警告的情况下进行编译。唯一的问题是这段代码:

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 不要只针对那段代码发出警告?

4

1 回答 1

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
$
于 2014-11-08T06:06:58.853 回答