繁体   English   中英

stdbool.h在哪里?

[英]Where is stdbool.h?

我想在我的系统上找到_Bool定义,因此对于缺少它的系统我可以实现它。 我在这里和其他网站上看到过各种各样的定义,但是想要检查系统的最终定义。

轻微的问题,因为我找不到_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

有些编译器不提供_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_ */
$ 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不是,通过编译没有包含的单个变量声明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM