_Bool和bool:我如何解决使用_Bool的C库的问题? [英] _Bool and bool: How do I solve the problem of a C library that uses _Bool?

查看:438
本文介绍了_Bool和bool:我如何解决使用_Bool的C库的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C中编写了一组数据结构和函数,其中一些使用_Bool数据类型。当我开始时,项目将是纯C.现在我正在使用基于C ++的GUI工具包调查,并将后端代码放入库中。

I've written a collection of data structures and functions in C, some of which use the _Bool data type. When I began, the project was going to be pure C. Now I am investigating using a C++ based GUI tool kit and have made the backend code into a library.

但是,编译C ++ GUI时,编译器发出以下错误:

However, when compiling the C++ GUI the following error is emitted by the compiler:

ISO C ++禁止没有类型<_ code>

ISO C++ forbids declaration of '_Bool' with no type

我最初以为我可以搜索&将 _Bool 替换为 bool 并创建:

I initially thought I could search & replace _Bool to bool and create:

/* mybool.h */
#ifndef MYBOOL_H
#define MYBOOL_H

typedef _Bool bool;

#endif /* MYBOOL_H */

使用_Bool

#ifdef __cplusplus
extern "C" {
#else
#include "mybool.h"
#endif

/* rest of header... */

直到我意识到这将使用一个布尔(C _Bool)数据类型编译库,并使用另一个(C ++ bool)链接库。实际上,这可能并不重要,但在理论上,它可能是(可能有一些模糊的系统,这样做这样做会导致宇宙内翻)。

Until I realized this would be compiling the library with one boolean (C _Bool) data type, and linking against the library using another (C++ bool). Practically, this might not matter, but theoretically, it probably does (there might be some obscure system somewhere which doing so like this causes the universe to turn inside out).

我想我可以只使用一个int和使用0为假和1为真,typedef它与 typedef int mybool ,但它似乎没有吸引力。

I suppose I could just use an int and use 0 for false and 1 for true, and typedef it with something like typedef int mybool, but it seems unattractive.

有没有更好的/惯用的/标准的方法来做到这一点?

Is there a better/idiomatic/standard way to do this?

推荐答案

!只需 #include< stdbool.h> 并使用 bool

这篇关于_Bool和bool:我如何解决使用_Bool的C库的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆