18

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
  • 4,867
  • 3
  • 32
  • 51

5 Answers5

12

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.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
  • I can always test the __cplusplus macro in my header... Mmmm yeah, well spotted. – James Morris Aug 24 '10 at 07:08
  • http://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. – Brooks Moses Jan 06 '13 at 04:32
8

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 4
    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 `int`s I don't see why it shouldn't say the same about `bool`. – magras Jul 26 '16 at 12:22
  • @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 `` adds does not equate to compatibility. – Neil Roy May 08 '18 at 19:04
  • 1
    @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. – magras May 10 '18 at 09:16
3

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
James Morris
  • 4,867
  • 3
  • 32
  • 51
  • 4
    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 Jan 06 '13 at 04:28
  • 1
    @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. – AnT stands with Russia Jun 07 '13 at 01:12
  • 10
    @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 Jun 07 '13 at 01:26
  • 1
    If you can't answer without explaining yourself and being helpful without insults, than you shouldn't be posting. – Neil Roy May 08 '18 at 18:58
  • 2
    @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. – Lightness Races in Orbit May 08 '18 at 19:03
2

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

-7

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

doron
  • 27,972
  • 12
  • 65
  • 103