与stdbool.h C接口
时间:2020-03-05 18:42:41 来源:igfitidea点击:
在一个项目中,我正在C ++和使用这样定义的stdbool.h的C库进行接口连接。
#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之间的大小是不一致的,因为bool在C ++中是再见,在C中是4.
有没有人对如何克服这个问题有任何建议而不求助于我目前的解决方案?
//#define bool _Bool #define bool unsigned char
这违反了stbool.h的C99标准
解决方案
回答
大小不是唯一在这里会不一致的东西。在C ++中,布尔是一个关键字,C ++保证布尔可以保持1或者0的值,而不能保留其他任何值。 C没有给我们这种保证。
就是说,如果C和C ++之间的互操作性很重要,则可以通过为C ++定义一个相同的布尔值并使用它代替内置的布尔值来模拟C的自定义布尔值。这将是错误的布尔值与C布尔值与C ++布尔值之间相同行为之间的折衷。
回答
逻辑上,我们无法在C和C ++之间共享带有冲突的bool声明的源代码,并使它们彼此链接。
共享代码和链接的唯一方法是通过中间数据结构。不幸的是,据我了解,我们无法修改定义C ++程序与C库之间接口的代码。如果可以的话,我建议我们使用类似的方法:
union boolean { bool value_cpp; int value_c; };
//根据字节顺序,可能需要填充
这样做的结果是使两种语言的数据类型具有相同的宽度;转换为本地数据类型将需要在两端进行。在库函数定义中将布尔值的布尔值替换为布尔值,在库中转换代码以进行转换,就可以了。
因此,我们要做的是在C ++程序和C库之间创建垫片。
你有:
extern "C" bool library_func_1(int i, char c, bool b);
我们需要创建:
bool library_func_1_cpp(int i, char c, bool b) { int result = library_func_1(i, c, static_cast<int>(b)); return (result==true); }
现在改为调用library_func_1_cpp。
回答
通过找到更符合C99标准的stdbool.h
实现,我找到了自己的问题的答案。
#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
这摘自Ada类库项目。