问题描述
在一个项目,我的C ++和使用定义为这样stdbool.h C库之间的接口。
In a project I am interfacing between C++ and a C library that uses stdbool.h defined as such.
#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
某些结构具有布尔
成员。所以,如果我定义为一个C ++函数内的局部变量这些结构之一,它传递给C函数的大小C ++和C之间不一致的布尔是下1轮空++和4℃。
Some structures have bool
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.
没有人有任何意见,如何克服这种不诉诸我目前的解决方案,这是
Does anyone have any advice to how to overcome this without resorting to my current solution which is
//#define bool _Bool
#define bool unsigned char
这是对C99标准
推荐答案
我找到了答案,以我自己的问题找到一个更兼容的实施 stdbool.h
即符合C99标准。
I found the answer to my own question by finding a more compatible implementation of stdbool.h
that is compliant with the C99 standard.
#ifndef _STDBOOL_H
#define _STDBOOL_H
#include <stdint.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 */
/* ISO C Standard: 5.2.5 An object declared as
type _Bool is large enough to store
the values 0 and 1. */
/* We choose 8 bit to match C++ */
/* It must also promote to integer */
typedef int8_t _Bool;
#endif
/* ISO C Standard: 7.16 Boolean type */
#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1
#endif
#endif
这是从的拍摄。
这篇关于与stdbool.hÇ++接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!