C中的#define和#include顺序
我有以下代码,我用
gcc编译
#include<stdio.h> #include<stdbool.h> #define true 9 int main() { printf("TRUE = %dn",true); return 0; } 我得到错误 test.c:3:0: warning: "true" redefined [enabled by default] In file included from test.c:2:0: /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stdbool.h:34:0: note: this is the location of the previous definition 但是当我改变代码的时候 #include<stdio.h> #define true 9 #include<stdbool.h> int main() { printf("TRUE = %dn",true); return 0; } 输出: TRUE = 1 题: 我明白第一种情况的错误的原因,但在第二种情况下,当我在#include< stdbool.h>之前定义true时,为什么允许重新定义true? 更新: 这是stdbool.h. 前几行是 #ifndef _STDBOOL_H #define _STDBOOL_H #ifndef __cplusplus #define bool _Bool #define true 1 #define false 0 这在余昊answer没有什么. 解决方法
虽然
Yu Hao给了
possible answer这个问题,但事情确实在这里不同了.
这很容易通过查看stdbool.h来实现,通过查看stdbool.h来了解,没有#ifdef … #endif围绕true的定义“守卫”. 此外,gcc只是简单地警告系统头文件中会出现哪些问题* 1. 使用gcc的选项-isystem使第一个例子表现得像第二个. From the gcc manual:
* 1:系统头文件包含在<>括号. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |