_Bool and bool: How to solve the problem of the C library that uses _Bool?

I wrote a set of data structures and functions in C, some of which use the _Bool data type. When I started, the project was supposed to be pure C. Now I study using the C ++ based GUI toolkit and have done the internal code in the library.

However, when compiling the C ++ GUI, the compiler emits the following error:

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

At first it seemed to me that I could find and replace _Bool with 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 that it would be collecting a library with one boolean (C _Bool) data type and linking to the library using another (C ++ bool). In practice, this may not be important, but theoretically it is possible (maybe some obscure system somewhere that makes the universe turn inside out).

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

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

+13
c ++ c boolean
Aug 20 '10 at 9:57
source share
5 answers

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

+3
Aug 20 '10 at 10:35
source share

If the C and C ++ compilers you use belong to the same provider, I would expect C _Bool be the same type as C ++ bool , and that including <stdbool.h> would make everything well compatible. If they belong to other manufacturers, you need to check compatibility.

Note. You can always check the __cplusplus macro in your header to determine if the code compiles as C ++ and set the types accordingly.

+11
Aug 20 '10 at 10:40
source share

Formally, there is no solution to this problem. The _Bool type exists only in C. C ++ does not provide any type that guarantees binary compatibility with _Bool . C ++ bool compatibility is not guaranteed.

The correct solution is not to use bool or _Bool in declarations of parameters of C functions intended for direct access (i.e. binding) to C ++ code. Use int , char or any other type that is guaranteed to be compatible.

+3
Jun 07 '13 at 1:10
source share

I think you need to use bool in any project in order to be compatible with C ++, if necessary.

The _Bool data _Bool on C99 exists to avoid conflicts with possible existing versions of bool that programmers could have defined before the standard C99. Thus, the programmer can choose the best way to adapt his old C programs to migration to the new standard. Thus, I believe that using the word bool is truly the desire of the C99 community, but he was forced to surround incompatibility issues by defining the ugly word _Bool . This shows that the programmer will probably have to use the β€œintended” word bool in his projects. This is the most logical word for declaring a boolean type in a program.

In some cases, it would be a good idea to keep the programming biased definition of bool , and in other cases it would be better to use the version of bool defined in <stdbool.h> . The programmer must decide what is best in each case, and besides, it is possible to think about whether gradual migration to <stdbool.h> good action.

If you are starting a project from scratch, then probably the best approach would always be to use the bool defined in <stdbool.h> .

+1
Jun 07 '13 at 0:16
source share

use typedef int _Bool for C and C ++ code. I will be surprised if _Bool is defined as anything else.

-four
Aug 20 '10 at 10:01
source share



All Articles