How to Integrate .Proto Files in Xcode Compilation

Compile protobuf with xCode 5

If you don't mind building Google Protobuf yourself then a good alternative to using Cocoapods is to run the bash script here.

https://gist.github.com/BennettSmith/7150245

This script will produce a proper build of Google Protobuf that supports the i386, armv7, armv7s, arm64 and x86_64 architectures. It will produce a static library that is universal. It will also produce the protoc compiler for use on OS X.

Google protocol buffers on iOS

Ok. It appears that the metasyntactic library (or any other 3rd party library) is unnecessary in this case. You can just add the Google source directly to your project. I found the following answer from Nicola Ferruzzi in a google discussion group . . .

The original answer is here . . .

http://groups.google.com/group/protobuf/browse_thread/thread/ca4218d7db144252

The content of this answer is included below with images to make a permanent record ...


EDIT

Since trying this again tonight for the first time in a while, I needed a couple more steps in addition to those outlined below (this works with protobuf 2.5.0).

  • You'll need to link against libz.dylib. You set this in Build Phases > Link Binary With Libraries.
  • To easily remove all of the unit test related stuff, use the following command from the shell in the google directory find . -name "*unittest*" -exec rm -rf {} \;
  • Also delete the folder called testing
  • comment out #include <google/protobuf/testing/googletest.h> in stringprintf.cc
  • Now carefully follow the instructions below and all should work fine.

I use latest version in my app .. you don't really need objc direct
support if you are familiar with C++, there is only one point where
you have to pass from std::string to NSData and viceversa. And its
pretty simple.

To compile and test the easiest way Ive found is to just import the
whole google directory in my own project :) (the second time you can
make your own framework but for testing this procedure just works)

  • download latest version
  • autogen configure and make like you were just building for macosx (you need command line tools) . This way you end up with protoc

    binary and the library for macosx (which you don't need)
  • open your Xcode iOS project
  • add "new file" to your project and select google directory
  • add the directory of google headers to your additional include directories
  • add config.h from the protobuffer src directory to your app
  • from the google group remove everything that contains unitest :)
  • from the google group remove compiler and java stuff;

You should be able to compile without any linking error. To give you
an idea this is what I directly compile

Sample Image

Then you can use protoc to generate c++ source files for your
protocol. To use them with objc you have to rename your source to
file "mm" then you can do something like

TO SERIALIZE TO NSDATA


let's say your message is called Packet

- (NSData *)getDataForPacket:(Packet *)packet { 
std::string ps = packet->SerializeAsString();
return [NSData dataWithBytes:ps.c_str() length:ps.size()];

TO READ FROM NSDATA

- (Packet *)getPacketFromNSData:(NSData *)data { 
char raw[[data length]];
Packet *p = new Packet;
[data getBytes:raw length:[data length]];
p->ParseFromArray(raw, [data length]);
return p;

}


Related Topics



Leave a reply



Submit