Where is stdbool.h?

后端 未结 6 1863
温柔的废话
温柔的废话 2020-12-28 15:42

I want to find the _Bool 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

相关标签:
6条回答
  • 2020-12-28 16:13

    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_ */
    
    0 讨论(0)
  • 2020-12-28 16:15

    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;
    
    0 讨论(0)
  • 2020-12-28 16:23

    _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
    
    0 讨论(0)
  • 2020-12-28 16:25

    _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.

    0 讨论(0)
  • 2020-12-28 16:36
    $ 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 讨论(0)
  • 2020-12-28 16:38

    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.

    0 讨论(0)
提交回复
热议问题