gpt4 book ai didi

c - gcc:修复 -pedantic "unnamed structure"警告

转载 作者:太空宇宙 更新时间:2023-11-03 23:48:33 28 4
gpt4 key购买 nike

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

最佳答案

您要按照哪个标准进行编译?

给定这段代码:

#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
$

关于c - gcc:修复 -pedantic "unnamed structure"警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26814236/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com