开发者

Where is stdbool.h?

I want to find the _Boo开发者_开发问答l definition on my system, so for systems where it's missing I can implement it. I've seen various definitions for it here and on other sites, but wanted to check on the system for the definitive definition.

Slight problem, in that I can't find where _Bool is defined or even stdbool.h

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

And grep for _Bool on /usr/include/* and /usr/include/*/* does not find it either.

So where is it?


_Bool is a built-in type, so don't expect to find a definition for it in a header file, even a system header file.

Having said that, guessing your system from the paths that you are searching, have you looked in /usr/lib/gcc/*/*/include ?

My "real" stdbool.h lives there. As expected it #defines bool to be _Bool. As _Bool is a type native to the compiler there's no definition for it in the header file.


As a note:

The _Bool is defined in C99. If you build your program with:

gcc -std=c99

You can expect it to be there.


Other people have replied to the question on _Bool location and finding if C99 is declared... however, I am not satisfied with the self-made declaration everyone gave.

Why won't you completely define the type?

typedef enum { false, true } bool;


_Bool is a predefined type in C99, much like int or double. You will not find the definition for int in any header file either.

What you can do is

  • check the compiler is C99
  • if it is use _Bool
  • otherwise use some other type (int or unsigned char)

For example:

#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


some compilers don't offer _Bool keywords, so I wrote my own 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’

This demonstrates that _Bool is a built-in type and bool is not, by compiling a single variable declaration with no includes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜