Discussion:
How to use extern C with _Bool
(too old to reply)
Frederick Gotham
2020-04-23 11:50:17 UTC
Permalink
I have a function in a C source file as follows:

#include <stdbool.h>

int EnableDebug(bool en_dbg)
{
/* Do Something */
return 0;
}

I compile this C source file to an object file. Later I want to use it in a C++ program, and so I need to declare this function in a C++ header. But which one do I use?

extern "C" int EnableDebug(bool);

or:

extern "C" int EnableDebug(int);

My understanding in C is that "bool" is a typedef for "_Bool". But is "_Bool" just a regular 'int' or is it something smaller (e.g. char) ?
Öö Tiib
2020-04-23 12:46:06 UTC
Permalink
Post by Frederick Gotham
#include <stdbool.h>
int EnableDebug(bool en_dbg)
{
/* Do Something */
return 0;
}
I compile this C source file to an object file. Later I want to use it in a C++ program, and so I need to declare this function in a C++ header. But which one do I use?
extern "C" int EnableDebug(bool);
extern "C" int EnableDebug(int);
My understanding in C is that "bool" is a typedef for "_Bool". But is "_Bool" just a regular 'int' or is it something smaller (e.g. char) ?
So use bool and require C code to #include <stdbool.h>. AFAIK C _Bool
and C++ bool are same type (in every ABI in practice) but I do not
know if it is formally guaranteed somewhere or not.
Chris Vine
2020-04-23 13:38:12 UTC
Permalink
On Thu, 23 Apr 2020 05:46:06 -0700 (PDT)
Post by Öö Tiib
Post by Frederick Gotham
#include <stdbool.h>
int EnableDebug(bool en_dbg)
{
/* Do Something */
return 0;
}
I compile this C source file to an object file. Later I want to use it in a C++ program, and so I need to declare this function in a C++ header. But which one do I use?
extern "C" int EnableDebug(bool);
extern "C" int EnableDebug(int);
My understanding in C is that "bool" is a typedef for "_Bool". But is "_Bool" just a regular 'int' or is it something smaller (e.g. char) ?
So use bool and require C code to #include <stdbool.h>. AFAIK C _Bool
and C++ bool are same type (in every ABI in practice) but I do not
know if it is formally guaranteed somewhere or not.
I should either (i) use int for both C and C++, or (ii) use _Bool/bool
for both C and C++, and not mix ints with bools. bool(0.5) is
different in both languages from int(0.5).

As I recall it when I looked into it there was no requirement for C's
_Bool to be the same size as C++'s bool, although it would be a brain
dead implementation which made them different sizes. As an aside, I
cannot now recall whether the incorporation of parts of C into C++ in
Annex C formally requires C's ints to be the same size as C++'s ints
but I think it is implicit from the incorporation of parts of C's
standard library into C++ that they must be.
David Brown
2020-04-23 13:54:56 UTC
Permalink
Post by Chris Vine
On Thu, 23 Apr 2020 05:46:06 -0700 (PDT)
Post by Öö Tiib
Post by Frederick Gotham
#include <stdbool.h>
int EnableDebug(bool en_dbg)
{
/* Do Something */
return 0;
}
I compile this C source file to an object file. Later I want to use it in a C++ program, and so I need to declare this function in a C++ header. But which one do I use?
extern "C" int EnableDebug(bool);
extern "C" int EnableDebug(int);
My understanding in C is that "bool" is a typedef for "_Bool". But is "_Bool" just a regular 'int' or is it something smaller (e.g. char) ?
So use bool and require C code to #include <stdbool.h>. AFAIK C _Bool
and C++ bool are same type (in every ABI in practice) but I do not
know if it is formally guaranteed somewhere or not.
I should either (i) use int for both C and C++, or (ii) use _Bool/bool
for both C and C++, and not mix ints with bools. bool(0.5) is
different in both languages from int(0.5).
As I recall it when I looked into it there was no requirement for C's
_Bool to be the same size as C++'s bool, although it would be a brain
dead implementation which made them different sizes. As an aside, I
cannot now recall whether the incorporation of parts of C into C++ in
Annex C formally requires C's ints to be the same size as C++'s ints
but I think it is implicit from the incorporation of parts of C's
standard library into C++ that they must be.
I expect that technically the C++ compiler is a separate implementation
from the C compiler, even when they are both part of the same package
(like gcc). In C++, "int" used by functions in the C library must match
"int" used by other parts of C++, but there is no requirement that it
match "int" used by a C compiler, or by any other compiler on the
system. The same applies between two C compilers on the same system.

But as you say, it would take a brain-dead implementation to end up with
anything different in "int" (or bool/_Bool) on a given platform. These
sorts of things are specified in platform ABI's and any compiler for C
or C++ will follow the ABI for the platform.

(I know of a couple of C compilers where the size of "int" can be
changed from the command line, but these won't be of concern to most
people.)
Cholo Lennon
2020-04-24 17:07:30 UTC
Permalink
Post by David Brown
(I know of a couple of C compilers where the size of "int" can be
changed from the command line, but these won't be of concern to most
people.)
I was reading something related today...

"LLVM Clang compiler adds support for custom width integers"
https://www.theregister.co.uk/2020/04/24/llvm_project_adds_support_for/


--
Cholo Lennon
Bs.As.
ARG
James Kuyper
2020-04-23 14:28:51 UTC
Permalink
On 4/23/20 9:38 AM, Chris Vine wrote:
...
Post by Chris Vine
As I recall it when I looked into it there was no requirement for C's
_Bool to be the same size as C++'s bool, although it would be a brain
dead implementation which made them different sizes. As an aside, I
cannot now recall whether the incorporation of parts of C into C++ in
Annex C formally requires C's ints to be the same size as C++'s ints
but I think it is implicit from the incorporation of parts of C's
standard library into C++ that they must be.
I agree. The C standard says nothing about interoperability with C++,
and the C++ standard says very little about interoperability with C.
However, <stdbool.h> and <cstdbool> are both supported headers in C++,
and are both prohibited from defining the macro named 'bool' that is a
mandatory feature of <stdbool.h> in C. There would be almost no point in
allowing those headers in C++, if C++ bool isn't compatible with C's
_Bool type.
Tim Rentsch
2020-04-25 07:31:41 UTC
Permalink
Post by Frederick Gotham
#include <stdbool.h>
int EnableDebug(bool en_dbg)
{
/* Do Something */
return 0;
}
I compile this C source file to an object file. Later I want to use
it in a C++ program, and so I need to declare this function in a C++
header. But which one do I use?
extern "C" int EnableDebug(bool);
extern "C" int EnableDebug(int);
My understanding in C is that "bool" is a typedef for "_Bool". But
is "_Bool" just a regular 'int' or is it something smaller (e.g.
char) ?
Just use int. You know int will work, without having to worry
about possible incongruities between _Bool/bool, headers, etc.
Also, if the parameter type is bool, then because of conversion
rules it would be allowed to pass pointers as arguments. My guess
is you don't want to allow passing pointers as arguments.
Alf P. Steinbach
2020-04-25 08:24:47 UTC
Permalink
Post by Frederick Gotham
#include <stdbool.h>
int EnableDebug(bool en_dbg)
{
/* Do Something */
return 0;
}
I compile this C source file to an object file. Later I want to use
it in a C++ program, and so I need to declare this function in a C++
header. But which one do I use?
extern "C" int EnableDebug(bool);
extern "C" int EnableDebug(int);
My understanding in C is that "bool" is a typedef for "_Bool". But
is "_Bool" just a regular 'int' or is it something smaller (e.g. char)?
Use the first one.

And if you feel it necessary, add something like

#ifdef __cplusplus
static_assert( c_bool_size = sizeof( bool ) );
#endif

where `c_bool_size` comes from a header generated in the build process,
or just manually written for each C/C++ compiler pair.

- Alf

Loading...