4

I'm trying to build protobuf c++ into ios. But its implementation contains a TYPE_BOOL enum variable that conflicts with TYPE_BOOL in ios macro. And compile failed.

How to solve this?

lseeo
  • 799
  • 2
  • 9
  • 15
  • Dont include that file from Obj-C, and wrap it around http://www.cocoabuilder.com/archive/cocoa/316738-nil-and-nil-macro-conflict.html – Anoop Vaidya Apr 02 '13 at 08:44

1 Answers1

2

There are a few reasonable (but hacky) options:

  1. #include any protobuf headers that use TYPE_BOOL before you #include any iOS headers. Example:

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

    This lets you use iOS's TYPE_BOOL in your own code, but not protobuf's TYPE_BOOL.

  2. #include the iOS header, then #undef TYPE_BOOL before you #include the protobuf header. Example:

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

    This lets you use protobuf's TYPE_BOOL in your own code, but not iOS's TYPE_BOOL.

  3. If you need both definitions, this might work (not tested):

    #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>
    

    Now use PROTOBUF_TYPE_BOOL when you need the Protobuf definitions.

Note that google/protobuf/descriptor.pb.h also defines a TYPE_BOOL. It can be solved in the same way, but most people don't use that header, so I left it out of the examples.

Kenton Varda
  • 41,353
  • 8
  • 121
  • 105