Compiling the Protocol Buffer Compiler for Objective-C
您目前处于:技术核心竞争力  2015-06-21

As we know, protocol buffer compiler will only spit out C++, Java, and Python headers, but not Objective-C.

So what do we do when it comes to Objective-C? Luckily, there's a open-source port by Cyrus Najmabadi that you can find here(http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers).

1. Creating the Protocol Buffer Compiler(protoc)

In order to compiler and use protoc you must first have installed XCode and the rest of Apple's developer tools.

Download and unzip the latest ProtocolBuffers-*.*-Source.tar.gz file in the download section to <install_directory> on your computer.

Navigate to <install_directory> and type the following commands:

$ ./autogen.sh
$ ./configure
$ make

protoc will then be built into the <install_directory>/src directory.

Note:

You need autoconf to compile from source code. If your system is not install autoconf, you can install it by brew:

$ brew install libtool 
$ brew install autoconf
$ brew install automake

If you protobuf in - objc compilation errors, given the information as follows :

google/protobuf/message.cc:130:60: error: implicit instantiation of undefined
      template 'std::__1::basic_istream<char, std::__1::char_traits<char>>'

You can locate the file src/google/protobuf/message.cc on the first row #include files added:

#include <istream>

Continue to make, and that is successfully passed.

2. Generate Object-C Code

sudo ./protoc --proto_path=/Users/zhangsr/Desktop/work/protobuf-ios/ 
--objc_out=/Users/zhangsr/Desktop/work/protobuf-ios/ 
/Users/zhangsr/Desktop/work/protobuf-ios/MessageBuf.proto

3. Integrate with source code

Drag /objectivec/ProtocolBuffers.xcodeproj to your Xcode project.

That's all.

Compiler setting

Since the generated code is non-ARC, if your project use ARC by default, you need add -fno-objc-arc to generated files.

In Xcode, select the main project/targets/build phases/compile sources, select the m files/double click under compiler flag/add -fno-objc-arc to the popped window.

References:

https://developers.google.com/protocol-buffers/docs/downloads
https://github.com/google/protobuf
http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers
http://code.google.com/p/metasyntactic/downloads/list
https://github.com/mingchen/protobuf-ios
http://brew.sh/


转载请并标注: “本文转载自 linkedkeeper.com ”  ©著作权归作者所有