【问题标题】:gcc: fixing -pedantic "unnamed structure" warninggcc:修复-pedantic“未命名结构”警告
【发布时间】:2014-11-08 05:51:10
【问题描述】:

我正在尝试从其他地方(特别是 here)获取一些代码,以便在 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 不要只针对那段代码发出警告?

【问题讨论】:

  • an answer to an SO post 看到这个。 #pragma warning (disable : 4068 )。也许在函数可能起作用之前类似的东西。
  • #pragma warning (disable : 4068 ) 似乎不适用于 gcc
  • 一个不同的警告编号,一个代表您试图阻止显示的警告,应该可以工作。
  • 不,我尝试了多个数字,但它们都给出了相同的警告信息,即忽略它。
  • 实际的、完整的警告信息是什么?

标签: c gcc struct compiler-warnings gcc-pedantic


【解决方案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
$

【讨论】:

  • 使用-std=c11 解决了我写的问题,但引入了它现在给implicit declaration of function ‘siginterrupt’ 的问题,即使我包括signal.h
  • 然后将其更改为-std=gnu11…这样可以在c11 变体不支持的情况下创建大多数其他声明。
猜你喜欢
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
相关资源
最近更新 更多