【问题标题】:interfacing with stdbool.h C++与 stdbool.h C++ 接口
【发布时间】:2010-09-06 17:30:59
【问题描述】:

在一个项目中,我在 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

一些结构有bool 成员。因此,如果我将这些结构之一定义为 C++ 函数中的局部变量并将其传递给 C 函数,则 C++ 和 C 之间的大小不一致,因为 bool 在 C++ 中是一个再见,在 C 中是 4。

有没有人对如何在不诉诸我目前的解决方案的情况下克服这个问题有任何建议

//#define bool _Bool
#define bool unsigned char

这违反了stdbool.h 的 C99 标准

【问题讨论】:

    标签: c++ c boolean standards


    【解决方案1】:

    通过找到符合 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 Class Library 项目。

    【讨论】:

    • 更新:_Bool 现在在 VS2013 中定义了,所以我们还需要检查 _MSC_VER msdn.microsoft.com/en-us/library/hh409293.aspx
    【解决方案2】:

    大小不是这里唯一不一致的东西。在 C++ 中,bool 是一个关键字,C++ 保证 bool 可以保存值 1 或 0,仅此而已。 C 不给你这个保证。

    也就是说,如果 C 和 C++ 之间的互操作性很重要,您可以通过为 C++ 定义一个相同的布尔值并使用它而不是内置的布尔值来模拟 C 的自定义布尔值。这将是一个有缺陷的布尔值与 C 布尔值和 C++ 布尔值之间的相同行为之间的权衡。

    【讨论】:

      【解决方案3】:

      从逻辑上讲,您不能在 C 和 C++ 之间共享具有冲突声明的 bool 源代码并让它们相互链接。

      您可以共享代码和链接的唯一方法是通过中间数据结构。不幸的是,据我了解,您无法修改定义 C++ 程序和 C 库之间接口的代码。如果可以的话,我建议使用类似的东西:

      union boolean {
         bool value_cpp;
         int  value_c;
      }; 
      

      // 根据endianness 可能需要填充

      其效果是使两种语言中的数据类型宽度相同;需要在两端执行转换为本机数据类型。将库函数定义中的 bool 换成 boolean 的使用,在库中摆弄代码进行转换,就大功告成了。

      因此,您需要做的是在 C++ 程序和 C 库之间创建一个shim

      你有:

      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。

      【讨论】:

        猜你喜欢
        • 2019-03-19
        • 2013-09-17
        • 2021-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-18
        • 2018-10-03
        相关资源
        最近更新 更多