[英]C preprocessor redefine conflict dependent on include order
I just had a redefine conflict in the project I'm working on and while tracing down why it's not happening on all platforms (turned out to be to order of includes), I stumbled upon the following behavior which I cannot explain. 我刚刚在我正在进行的项目中有一次重新定义的冲突,并且在追踪为什么它没有在所有平台上发生(原来是按包含顺序)时,我偶然发现了以下无法解释的行为。
#define LIST_HEAD(a) { int a = 0; }
#include <sys/queue.h>
int main() {
return 0;
}
#include <sys/queue.h>
#define LIST_HEAD(a) { int a = 0; }
int main() {
return 0;
}
I would expect both cases to produce the warning, since there're no checks in <sys/queue.h>
that would prevent a redefine. 我希望这两种情况都能产生警告,因为
<sys/queue.h>
没有可以阻止重新定义的检查。
So why does the first case produces no warning, while the second one does? 那么为什么第一种情况不产生警告,而第二种情况呢? What I'm missing here?
我在这里缺少什么?
Btw: I get the same results on my Mac with clang and my Linux box with gcc. 顺便说一句:我在Mac上用clang和我的Linux机箱用gcc得到了相同的结果。
By default, this warning is suppressed in system headers. 默认情况下,系统标头中会禁止此警告。 The code in
<sys/queue.h>
is considered to come from a system header because sys/queue.h was found by searching a path marked as containing system headers. <sys/queue.h>
的代码被认为来自系统头,因为通过搜索标记为包含系统头的路径找到了sys / queue.h 。
So in (2) you see the warning because it is generated within your code, while in (1) the warning is generated within queue.h , and so is suppressed. 因此,在(2)中,您会看到警告,因为它是在您的代码中生成的,而在(1)中,警告是在queue.h中生成的,因此被抑制。 Add
-Wsystem-headers
to your compilation options, and you'll see the warning in both cases. 将
-Wsystem-headers
添加到编译选项中,在两种情况下都会看到警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.