我正在做一个 gcc 编译-Wlto-type-mismatch
和-Werror
设置(为了项目的其余部分)。我有extern
struct
一个灵活的数组,它会引发lto-type-mismatch
警告/错误。
以下是将代码简化为示例:
呵呵:
typedef struct {
int i;
int ints[];
} struct_t;
交流:
#include "h.h"
extern struct_t my_struct;
int main() { // just here to avoid optimizing away the decls
return my_struct.ints[0];
}
公元前:
#include "h.h"
struct_t my_struct = {
20,
{
1,
2,
},
};
编译(此处使用 arm gcc,但使用本机 gcc 也会失败)
$ arm-none-eabi-gcc -flto -Wlto-type-mismatch -Werror a.c b.c -o foo
a.c:3:17: error: size of 'my_struct' differ from the size of original declaration [-Werror=lto-type-mismatch]
extern struct_t my_struct;
^
b.c:3:10: note: 'my_struct' was previously declared here
struct_t my_struct = {
^
lto1: all warnings being treated as errors
我尝试将一个或两个声明包装在
#pragma gcc diagnostic push
#pragma gcc diagnostic ignore "-Wlto-type-mismatch"
<decl>
#pragma gcc diagnostic pop
但我无法抑制警告,可能是因为它是链接时优化,并且此时#pragma
线路早已消失。
问题
你能建议一些方法来编译它并在其他地方保留警告吗?(或者-Wlto-type-mismatch
不应该抱怨灵活数组?如果是这样,我可以提交错误报告。)