定义 bool 时出现 SWIG 语法错误

SWIG syntax error when defining bool

SWIG 无法处理以下模块,错误消息为 "example.i:2: Error: Syntax error in input(1)"

%module example
typedef unsigned char bool;
bool isEven(int number);

bool 更改为 boll 时一切正常。 SWIG 似乎将 bool 视为 C 中的内置类型。但是,只有 _Bool 是 C99 中的内置类型,除非包含 stdbool.h

,否则不会定义 bool

我有一个大型接口,需要为 java 包装,其中包含以下类型定义:

#ifndef __cplusplus
typedef unsigned char bool;
#endif

我可以通过添加

来解决这些问题
#define __cplusplus

添加到我的 SWIG 模块文件,然后再包含 header 定义接口。 但是,在这种情况下,包括 cpointer.i 不再起作用(错误:#ifdef 的标识符丢失,出现在包含 #ifdef __cplusplus 的 cpointer.i 的每一行)

关于如何使用我的界面 header 文件而不更改它(即不删除我的 bool typedef)并且仍然能够使用 cpointer.i 的任何建议?

如果 bool 在您的情况下确实没有定义,那么在 SWIG 接口文件中使用宏将其重命名为其他名称应该没有问题,即 do:

%module example
#define bool uchar_bool
typedef unsigned char bool;
bool isEven(int number);