是否有会警告使用未申报任何定义的C编译器

是否有会警告使用未申报任何定义的C编译器

本文介绍了是否有会警告使用未申报任何定义的C编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况最近传来的地方下面的结构

I came across the situation recently where the following construct

#if BYTE_ORDER == LITTLE_ENDIAN
   do_something();
#endif

在结果'do_something()'如果既 BYTE_ORDER 也不 LITTLE_ENDIAN 定义被编译。虽然这不是没有道理的行为,我无法找到任何gcc的选项,给我一个警告,在这种情况下。

results in 'do_something()' being compiled if neither BYTE_ORDER nor LITTLE_ENDIAN are defined. Whilst this isn't unreasonable behaviour, I can't find any option on gcc to give me a warning in this situation.

如果没有警告就可以进入,而令人担忧的情况下有人可以删除一个显然未使用的报头,将彻底改变编译的结果,因为它造成被列入该定义的这两个宏的标题(和界定他们不同)。

Without a warning you can get into the rather worrying situation where someone can remove an apparently unused header and it will completely change the result of the compilation, because it caused to be included a header that defined those two macros (and defined them differently).

推荐答案

男人的gcc

-Wundef
    Warn if an undefined identifier is evaluated in an #if directive.

因此​​:

echo -e '#if BYTE_ORDER == LITTLE_ENDIAN\n#endif'|gcc -E - -Wundef

打印:

# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "<stdin>"
<stdin>:1:5: warning: "BYTE_ORDER" is not defined [-Wundef]
<stdin>:1:19: warning: "LITTLE_ENDIAN" is not defined [-Wundef]

这篇关于是否有会警告使用未申报任何定义的C编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 21:51