当一个结构的初始化与它的结构类型定义不匹配时,即使编译器被认为是以最挑剔的方式运行,我也有一个gcc不产生错误的问题。
下面的命令由make在一个包中调用,该包配置为"developer模式“--即添加了一个选项,这样编译器就会尽可能挑剔和敏感--从而让开发人员知道代码中有些东西需要修复。
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它(目前)没有错误地完成所有的编译和构建,包括下面的块引号.
static chbclib_strq_cl st_mainclass =
{
st_meth_add // m_add
};..。它引用包中其他地方的包含文件中的下列结构.
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;当然,到目前为止,没有错误的东西建立是没问题的。但是,当我对包含-文件进行以下更改时,它应该会生成一个错误
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满足这一重要需求?
发布于 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。
https://stackoverflow.com/questions/28952053
复制相似问题