gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:56:07 28 4
gpt4 key购买 nike

我正在尝试将 protobuf c++ 构建到 ios 中。但是它的实现包含一个 TYPE_BOOL 枚举变量,它与 ios 宏中的 TYPE_BOOL 冲突。编译失败。

如何解决?

最佳答案

有一些合理的(但很老套)选项:

  1. #include 使用 TYPE_BOOL 的任何 protobuf header 在您#include 任何 iOS header 之前。示例:

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

    这让您可以在自己的代码中使用 iOS 的 TYPE_BOOL,但不能使用 protobuf 的 TYPE_BOOL

  2. #include iOS header ,然后是 #undef TYPE_BOOL#include protobuf header 之前。示例:

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

    这让您可以在自己的代码中使用 protobuf 的 TYPE_BOOL,但不能使用 iOS 的 TYPE_BOOL

  3. 如果您需要这两个定义,这可能有效(未测试):

    #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。它可以用同样的方式解决,但大多数人不使用该 header ,所以我将其排除在示例之外。

关于c++ - 名为 TYPE_BOOL(c++ 代码)的变量与 ios 宏冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15759559/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com