Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

boolean

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?

like image 380
James Morris Avatar asked Aug 20 '10 09:08

James Morris


People also ask

What library includes bool in C?

The C programming language, as of C99, supports Boolean arithmetic with the built-in type _Bool (see _Bool). When the header <stdbool. h> is included, the Boolean type is also accessible as bool .

Which header file is used for Bool?

To use boolean, a header file stdbool. h must be included to use bool in C. bool is an alias to _Bool to avoid breaking existing C code which might be using bool as an identifier.

Should I use Stdbool H?

h was added to allow users the obvious name. That way, if your code didn't have a home-brewed bool , you could use the built in one. So do indeed use stdbool. h if you aren't bound to some existing home-brewed bool .

What does Stdbool H do?

Using the system header file stdbool. h allows you to use bool as a Boolean data type. true evaluates to 1 and false evaluates to 0 . bool is just a nice spelling for the data type _Bool .


2 Answers

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.

like image 184
Anthony Williams Avatar answered Sep 16 '22 12:09

Anthony Williams


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.

like image 23
AnT Avatar answered Sep 19 '22 12:09

AnT