哪里是stdbool.h?(Where is stdbool.h?)

2019-09-03 09:45发布

我想找到_Bool我的系统上定义,因此对于系统中它的失踪,我可以实现它。 我已经看到了在这里和其他网站不同的定义,但要检查系统上的明确定义。

轻微的问题,我找不到在哪里_Bool定义,甚至stdbool.h

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

grep用于_Bool/usr/include/*/usr/include/*/*不要么找到它。

那么,是什么呢?

Answer 1:

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

说了这么多,猜测从您正在搜索的路径你的系统,你在看着/usr/lib/gcc/*/*/include

我的“真实” stdbool.h住在这里。 正如预期的那样它#define小号bool_Bool 。 作为_Bool是一种原产于编译器有一个在头文件中没有定义它。



Answer 2:

作为一个说明:

该_Bool在C99定义。 如果你建立你的程序有:

gcc -std=c99

你可以期望它在那里。



Answer 3:

其他人都回答了关于这个问题的_Bool位置,并发现如果C99声明...但是,我并不满足于自制声明人人都献出。

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

typedef enum { false, true } bool;


Answer 4:

_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


Answer 5:

一些编译器不提供_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_ */


Answer 6:

$ echo '_Bool a;' | gcc -c -x c -
$ echo $?
0

$ echo 'bool a;' | gcc -x c -c -
<stdin>:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘a’

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



文章来源: Where is stdbool.h?