名为TYPE_BOOL(c代码)的变量与ios宏冲突

我正在尝试将protobuf c构建到ios中.

但它的实现包含一个TYPE_BOOL枚举变量,该变量与ios宏中的TYPE_BOOL冲突.编译失败.

怎么解决这个?

最佳答案 有一些合理的(但是hacky)选项:

> #include任何在#include任何iOS标头之前使用TYPE_BOOL的protobuf标头.例:

#include <google/protobuf/descriptor.h>
#include <ConditionalMacros.h>

这允许您在自己的代码中使用iOS的TYPE_BOOL,而不是protobuf的TYPE_BOOL.
> #include iOS标题,然后在#include protobuf标题之前输入#undef TYPE_BOOL.例:

#include <ConditionalMacros.h>
#undef TYPE_BOOL
#include <google/protobuf/descriptor.h>

这使您可以在自己的代码中使用protobuf的TYPE_BOOL,而不是iOS的TYPE_BOOL.
>如果您需要两个定义,这可能有效(未经测试):

#include <google/protobuf/descriptor.h>

// Make a copy of TYPE_BOOL before it is hidden by a macro.
const google::protobuf::FieldDescriptor::Type PROTOBUF_TYPE_BOOL =
    google::protobuf::FieldDescriptor::TYPE_BOOL;

#include <ConditionalMacros.h>

现在,在需要Protobuf定义时使用PROTOBUF_TYPE_BOOL.

请注意,google / protobuf / descriptor.pb.h还定义了TYPE_BOOL.它可以以相同的方式解决,但大多数人不使用该标题,所以我把它留在了示例中.

点赞