首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当结构初始化与结构类型定义不匹配时,如何让GCC给出错误消息?

当结构初始化与结构类型定义不匹配时,如何让GCC给出错误消息?
EN

Stack Overflow用户
提问于 2015-03-09 21:26:18
回答 1查看 85关注 0票数 1

当一个结构的初始化与它的结构类型定义不匹配时,即使编译器被认为是以最挑剔的方式运行,我也有一个gcc不产生错误的问题。

下面的命令由make在一个包中调用,该包配置为"developer模式“--即添加了一个选项,这样编译器就会尽可能挑剔和敏感--从而让开发人员知道代码中有些东西需要修复。

代码语言:javascript
复制
gcc -Wall -Qunused-arguments -Werror -I./libchbclib/inc -I/Users/red_angel/chorebox_sys/include -c -o tmp/lchbclib/chbclib_strq_new.o ./libchbclib/csrc/chbclib_strq_new.c

它(目前)没有错误地完成所有的编译和构建,包括下面的块引号.

代码语言:javascript
复制
static chbclib_strq_cl st_mainclass =
{
  st_meth_add // m_add
};

..。它引用包中其他地方的包含文件中的下列结构.

代码语言:javascript
复制
typedef struct chbclib_strq_cl {
  bool (*m_add) ( void  *srf_aa, char *rg_a );
  // Adds a new string to the queue. Failure to do so is a
  // fatal-error to the program if the objects 'erat' value
  // is 0. Other wise, the boolean return value will let
  // the calling program know whether or not the operation
  // was a success.
} chbclib_strq_cl;

当然,到目前为止,没有错误的东西建立是没问题的。但是,当我对包含-文件进行以下更改时,它应该会生成一个错误

代码语言:javascript
复制
typedef struct chbclib_strq_cl {
  bool (*m_add) ( void  *srf_aa, char *rg_a );
  // Adds a new string to the queue. Failure to do so is a
  // fatal-error to the program if the objects 'erat' value
  // is 0. Other wise, the boolean return value will let
  // the calling program know whether or not the operation
  // was a success.

  bool (*m_axd) ( void  *srf_aa, char *rg_a );
  // Adding this to the structure-type definition should produce
  // an error ---- but it doesn't.

} chbclib_strq_cl;

-但出于某种原因,它仍然不会产生错误。

如果结构的初始化与结构类型定义不匹配,我是否可以强迫gcc编译器生成错误?谢谢。

当然--你们中的一些人可能会想,为什么我会抱怨错误信息没有出现。答案是--当我在非开发者模式下构建软件包时,我不介意错误的存在。但是,我具体地实现了开发模式选项,因为我希望尽可能多地注意代码中的任何问题。

在本例中,示例是,如果我有一个在程序不同部分初始化了该类型的多个结构的结构类型,并且向该结构类型添加了一个字段(或进行任何其他更改),那么我还需要在程序中初始化该类型的结构的每个其他位置更新代码。但是,如果我在更新中“漏掉了一个点”呢?如果发生这种情况,我就指望一条错误信息来提醒我。

那么--当我以开发人员模式配置包时,如何让gcc满足这一重要需求?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-09 21:48:58

您可能在寻找-Wmissing initializers选项。

默认情况下,在使用-Wextra时启用它。

gcc版4.8.2手册:

-Wmissing-field-initializers Warn if a structure's initializer has some fields missing. For example, the following code causes such a warning, because "x.h" is implicitly zero: struct s { int f, g, h; }; struct s x = { 3, 4 }; This option does not warn about designated initializers, so the following modification does not trigger a warning: struct s { int f, g, h; }; struct s x = { .f = 3, .g = 4 }; This warning is included in -Wextra. To get other -Wextra warnings without this one, use -Wextra -Wno-missing-field-initializers.

当使用此选项时,Clang将比GCC更频繁地触发警告。如果GCC没有发出警告,试着和Clang一起看看是否有什么变化。

在gcc版本4.8.2中,有些情况下没有触发警告(例如,只有第一个字段初始化为零),但也要注意,根据编译代码所用的标准,在第一个结构字段之前未初始化的结构字段可以接受为有效的C。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28952053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档