Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

c++ - Google protocol buffers on iOS

Is the metasyntactic static library for iOS . . .

http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers

. . . compatible with regular old C++ compiled protofiles? I do not want to use the bundled compiler that generates Obj-C.

Is there any way to compile the library supplied by Google for iOS?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here

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; 

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...