_Bool and bool: How do I solve the problem of a C library that uses _Bool?

14,792

Solution 1

Just #include <stdbool.h> and use bool.

Solution 2

If the C and C++ compilers you are using are from the same vendor then I would expect the C _Bool to be the same type as C++ bool, and that including <stdbool.h> would make everything nicely interoperable. If they are from different vendors then you'll need to check for compatibility.

Note, you can always test the __cplusplus macro in your header to determine whether or not the code is being compiled as C++, and set the types appropriately.

Solution 3

Formally, there's no solution for this problem. Type _Bool exists only in C. C++ language does not provide any type that would guarantee binary compatibility with _Bool. C++ bool is not guaranteed to be compatible.

The proper solution is not to use bool or _Bool in parameter declarations of C functions that are intended to be directly accessible (i.e. linkable) from C++ code. Use int, char or any other type that is guaranteed to be compatible.

Solution 4

I think that one must use bool in any project, in order to have compatibility with C++, if neccessary.

The data type _Bool in C99 exists to avoid conflicts with possible existent versions of bool that programmers could have been defined before the standard C99. Thus, the programmer can choose the better way to adapt their old C programs to migrate toward the new standard. Thus, I think that to use the word bool is the truly desire of C99 standarization commitee, but it have been forced to surround incompatibility issues by defining the ugly word _Bool. This shows that the programmer probably have to use the "intended" word bool in his/her projects. It is the most logical word to declare a boolean type in a program.

In some cases, it could be good idea to keep a programmer preexistent definition of bool and, in other cases, it would be better to use the bool version defined in <stdbool.h>. The programmer have to decide what is better in each case and besides, maybe, to consider if a gradual migration to <stdbool.h> is a good action to take.

If you are starting a project from zero, then probably the best approach is ever to use bool defined in <stdbool.h>.

Share:
14,792
James Morris
Author by

James Morris

Used to be a hobbyist coder with aspirations for a dev career. Didn't happen. Now most of my energy goes into commuting on a mountain bike with a bit of XC racing thrown in for good measure. Veteran category.

Updated on June 18, 2022

Comments

  • James Morris
    James Morris almost 2 years

    I've written a collection of data structures and functions in C, some of which use the _Bool data type. When I began, the project was going to be pure C. Now I am investigating using a C++ based GUI tool kit and have made the backend code into a library.

    However, when compiling the C++ GUI the following error is emitted by the compiler:

    ISO C++ forbids declaration of '_Bool' with no type

    I initially thought I could search & replace _Bool to bool and create:

    /* mybool.h */
    #ifndef MYBOOL_H
    #define MYBOOL_H
    
    typedef _Bool bool;
    
    #endif /* MYBOOL_H */
    

    and then in any headers that use _Bool

    #ifdef __cplusplus
    extern "C" {
    #else
    #include "mybool.h"
    #endif
    
    /* rest of header... */
    

    Until I realized this would be compiling the library with one boolean (C _Bool) data type, and linking against the library using another (C++ bool). Practically, this might not matter, but theoretically, it probably does (there might be some obscure system somewhere which doing so like this causes the universe to turn inside out).

    I suppose I could just use an int and use 0 for false and 1 for true, and typedef it with something like typedef int mybool, but it seems unattractive.

    Is there a better/idiomatic/standard way to do this?

  • James Morris
    James Morris over 13 years
    _Bool is the C99 native type.
  • James Morris
    James Morris over 13 years
    I can always test the __cplusplus macro in my header... Mmmm yeah, well spotted.
  • Brooks Moses
    Brooks Moses over 11 years
    Your question is still entirely valid, as C99's "bool" is just a #define's alias of "_Bool", and the question of whether it's compatible with C++'s "bool" is indeed a relevant one. Thus, I'm downvoting your self-answer. :)
  • Brooks Moses
    Brooks Moses over 11 years
    stackoverflow.com/questions/25461/interfacing-with-stdbool-h‌​-c is an example of a case where they aren't compatible, though the problem there was a bad non-compiler-provided stdbool.h header.
  • AnT stands with Russia
    AnT stands with Russia almost 11 years
    @James Morris: This answer makes no sense whatsoever. In C bool is just a macro substitute for _Bool, while in C++ it is a C++-specific type. There's no guarantee of C _Bool vs. C++ bool compatibility.
  • Keith Thompson
    Keith Thompson almost 11 years
    @AndreyT: There doesn't seem to be any guarantee that C int is compatible with C++ int. "Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent. Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved.". I'd expect C and C++ compilers intended to be used together (such as gcc and g++) to make their bool and int types, among others, compatible.
  • Keith Thompson
    Keith Thompson almost 11 years
    @JamesMorris: Not only is it the native type, its name is a keyword (assuming the C compiler supports C99 or later).
  • magras
    magras almost 8 years
    I doubt that int or char guaranteed to be compatible between C and C++. It can be stated to be true in documentation for compiler, but in general this assertion is false. And if documentation states compatible ints I don't see why it shouldn't say the same about bool.
  • Neil Roy
    Neil Roy almost 6 years
    If you can't answer without explaining yourself and being helpful without insults, than you shouldn't be posting.
  • Lightness Races in Orbit
    Lightness Races in Orbit almost 6 years
    @NeilRoy: Look at the OP's username. Now look at the answer's username. Now look at the answer's post date. Now ask yourself whether your rude comment achieves anything.
  • Neil Roy
    Neil Roy almost 6 years
    @magras: No, C++ was originally created before _Bool was a part of C, so there is no reason why it should be compatible at all with later revisions. int and char however are a part of standard C which C++ is based off of, so it absolutely should be compatible. _Bool was not added to C until the C99 standard, so unless otherwise stated, there is no reason why it should be compatible and it is reasonable to assume it is not. Similar naming conventions as <stdbool.h> adds does not equate to compatibility.
  • magras
    magras almost 6 years
    @NeilRoy: AFAIK current C++ standard based on C99. But does C++ standard requires binary compatibility with C? If not, I'd say you are still wrong and binary compatibility is implementation defined, so only documentation can answer this question.