<p>In a project I am interfacing between C++ and a C library that uses stdbool.h defined as such.</p>

<pre><code>#ifndef _STDBOOL_H
#define _STDBOOL_H

/* C99 Boolean types for compilers without C99 support */
/* http://www.opengroup.org/onlinepubs/009695399/basedefs/stdbool.h.html */
#if !defined(__cplusplus)

#if !defined(__GNUC__)
/* _Bool builtin type is included in GCC */
typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
#endif

#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1

#endif

#endif
</code></pre>

<p>Some structures have <code>bool</code> members. So if I have one of these structures defined as local variables within a C++ function and pass it to a C function the sizes are inconsistent between C++ and C as bool is one bye in C++ and 4 in C.</p>

<p>Does anyone have any advice to how to overcome this without resorting to my current solution which is</p>

<pre><code>//#define bool _Bool
#define bool unsigned char
</code></pre>

<p>Which is against the C99 standard for <a href="http://www.opengroup.org/onlinepubs/000095399/basedefs/stdbool.h.html" rel="noreferrer">stdbool.h</a></p>

[{'answer_id': 25482, 'author': 'wilhelmtell', 'author_id': 456, 'author_profile': 'https://Stackoverflow.com/users/456', 'pm_score': 2, 'selected': False, 'text': "<p>Size is not the only thing that will be inconsistent here.  In C++ bool is a keyword, and C++ guarantees that a bool can hold a value of either 1 or 0 and nothing else.  C doesn't give you this guarantee.</p>\n\n<p>That said, if interoperability between C and C++ is important you can emulate C's custom-made boolean by defining an identical one for C++ and using that instead of the builtin bool.  That will be a tradeoff between a buggy boolean and identical behaviour between the C boolean and the C++ boolean.</p>\n"}, {'answer_id': 25658, 'author': 'Josh', 'author_id': 257, 'author_profile': 'https://Stackoverflow.com/users/257', 'pm_score': 0, 'selected': False, 'text': '<p>Logically, you are not able to share source code between C and C++ with conflicting declarations for bool and have them link to each other.</p>\n\n<p>The only way you can share code and link is via an intermediary datastructure.  Unfortunately, from what I understand, you can\'t modify the code that defines the interface between your C++ program and C library.  If you could, I\'d suggest using something like:</p>\n\n<pre><code>union boolean {\n   bool value_cpp;\n   int  value_c;\n}; \n</code></pre>\n\n<p>// padding may be necessary depending on <a href="http://en.wikipedia.org/wiki/Endianness" rel="nofollow noreferrer">endianness</a> </p>\n\n<p>The effect of which will be to make the datatype the same width in both languages; conversion to the native data type will need to be performed at both ends.  Swap the use of bool for boolean in the library function definition, fiddle code in the library to convert, and you\'re done.</p>\n\n<p>So, what you\'re going to have to do instead is create a <a href="http://en.wikipedia.org/wiki/Shim_(computing)" rel="nofollow noreferrer">shim</a> between the C++ program and the C library.</p>\n\n<p>You have:</p>\n\n<pre><code>extern "C" bool library_func_1(int i, char c, bool b);\n</code></pre>\n\n<p>And you need to create:</p>\n\n<pre><code>bool library_func_1_cpp(int i, char c, bool b)\n{\n   int result = library_func_1(i, c, static_cast&lt;int&gt;(b));\n   return (result==true);\n}\n</code></pre>\n\n<p>And now call library_func_1_cpp instead.</p>\n'}, {'answer_id': 29300, 'author': 'JProgrammer', 'author_id': 1675, 'author_profile': 'https://Stackoverflow.com/users/1675', 'pm_score': 5, 'selected': True, 'text': '<p>I found the answer to my own question by finding a more compatible implementation of <code>stdbool.h</code> that is compliant with the C99 standard.</p>\n\n<pre><code>#ifndef _STDBOOL_H\n#define _STDBOOL_H\n\n#include &lt;stdint.h&gt;\n\n/* C99 Boolean types for compilers without C99 support */\n/* http://www.opengroup.org/onlinepubs/009695399/basedefs/stdbool.h.html */\n#if !defined(__cplusplus)\n\n#if !defined(__GNUC__)\n/* _Bool builtin type is included in GCC */\n/* ISO C Standard: 5.2.5 An object declared as \ntype _Bool is large enough to store \nthe values 0 and 1. */\n/* We choose 8 bit to match C++ */\n/* It must also promote to integer */\ntypedef int8_t _Bool;\n#endif\n\n/* ISO C Standard: 7.16 Boolean type */\n#define bool _Bool\n#define true 1\n#define false 0\n#define __bool_true_false_are_defined 1\n\n#endif\n\n#endif\n</code></pre>\n\n<p>This is taken from the <a href="http://sourceforge.net/projects/adacl" rel="nofollow noreferrer">Ada Class Library</a> project.</p>\n'}]
['https://Stackoverflow.com/questions/25461', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1675/']