stdbool.h在哪里?

我想在我的系统上find_Bool定义,所以对于缺less的系统我可以实现它。 我已经在这里和其他网站上看到了它的各种定义,但是想要检查系统是否有明确的定义。

有点问题,因为我找不到_Bool被定义在哪里,甚至是stdbool.h

 mussys@debmus:~$ find /usr/include/* -name stdbool.h /usr/include/c++/4.3/tr1/stdbool.h 

而且/usr/include/*/usr/include/*/*上的_Bool grep也找不到它。

那么它在哪里?

_Bool是一个内置类型,所以不要指望在头文件中找到它的定义,甚至是系统头文件。

话虽如此,从你正在搜索的路径猜测你的系统,你看了/usr/lib/gcc/*/*/include

我的“真正的” stdbool.h居住在那里。 正如所料,# #definebool_Bool 。 由于_Bool是编译器的本地类型,因此在头文件中没有定义它。

作为一个说明:

_Bool在C99中定义。 如果您使用以下方式构建程序:

 gcc -std=c99 

你可以期待它在那里。

其他人已经回答了关于_Bool位置的问题,并且发现是否宣布了C99 …但是,我不满意每个人给出的自制声明。

为什么你不完全定义类型?

 typedef enum { false, true } bool; 

_Bool是C99中的预定义类型,非常类似于intdouble 。 你不会在任何头文件中找到int的定义。

你可以做的是

  • 检查编译器是C99
  • 如果是使用_Bool
  • 否则使用其他类型( intunsigned char

例如:

 #if defined __STDC__ && defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L /* have a C99 compiler */ typedef _Bool boolean; #else /* do not have a C99 compiler */ typedef unsigned char boolean; #endif 
 $ echo '_Bool a;' | gcc -c -xc - $ echo $? 0 $ echo 'bool a;' | gcc -xc -c - <stdin>:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'a' 

这表明, _Bool是一个内置的类型, bool不是,通过编译一个单独的变量声明没有包含。

有些编译器不提供_Bool关键字,所以我写了自己的stdbool.h:

 #ifndef STDBOOL_H_ #define STDBOOL_H_ /** * stdbool.h * Author - Yaping Xin * E-mail - xinyp at live dot com * Date - February 10, 2014 * Copyright - You are free to use for any purpose except illegal acts * Warrenty - None: don't blame me if it breaks something * * In ISO C99, stdbool.h is a standard header and _Bool is a keyword, but * some compilers don't offer these yet. This header file is an * implementation of the stdbool.h header file. * */ #ifndef _Bool typedef unsigned char _Bool; #endif /* _Bool */ /** * Define the Boolean macros only if they are not already defined. */ #ifndef __bool_true_false_are_defined #define bool _Bool #define false 0 #define true 1 #define __bool_true_false_are_defined 1 #endif /* __bool_true_false_are_defined */ #endif /* STDBOOL_H_ */