iOS上的Google协议缓冲区 [英] Google protocol buffers on iOS

查看:157
本文介绍了iOS上的Google协议缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是iOS的元动态静态库。 。 。

Is the metasyntactic static library for iOS . . .

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

。 。 。兼容常规的老C ++编译原始文件?我 要使用生成Obj-C的捆绑编译器。

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

有没有办法编译Google for iOS提供的库?

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

推荐答案

好的。看起来在这种情况下,元理论库(或任何其他第三方库)是不必要的。您只需将Google来源直接添加到您的项目中即可。我在Nicola Ferruzzi的一个google讨论组中发现了以下答案。 。 。

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

今天晚上第一次,我需要几个步骤除了以下概述(这与protobuf 2.5.0)。

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).


  • 您需要链接到libz.dylib。您可以在Build Phases> Link Binary With Libraries中设置此项。

  • 要轻松删除所有与单元测试相关的内容,请使用google目录中的shell中的以下命令查找。 -name* unittest *-exec rm -rf {} \;

  • 同时删除名为 testing

  • #include< google / protobuf / testing / googletest.h> > stringprintf.cc

  • 现在仔细按照以下说明操作,所有操作都可以正常工作。

  • 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.

我在我的应用程序中使用最新版本..你不需要objc直接
支持熟悉C ++,只有一个点,
你必须从std :: string传递给NSData和反向。它的
很简单。

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.

要编译和测试最简单的方法,我发现只是导入
整个google目录在我自己的项目:)(第二次你可以
制作自己的框架,但测试此过程只是工作)

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)


  • 下载最新版本

  • autogen配置和使你喜欢你刚刚构建macosx(你需要命令行工具)。这样,你最终得到了原型

    二进制和macosx的库(你不需要它)。

  • 打开你的Xcode iOS项目

  • 在您的项目中添加新文件并选择google目录

  • 将google标题目录添加到您的其他目录

  • 将config.h从protobuffer src目录添加到您的应用程序

  • 从google组中删除包含unitest的所有内容:)

  • 编译器和java东西;

  • 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

然后您可以使用protoc为您的
协议生成c ++源文件。要使用它们与objc,你必须重命名你的来源为
文件mm,那么你可以做一些像

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

我们假设你的消息叫做Packet

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()]; 



从NSDATA读取



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; 

}


这篇关于iOS上的Google协议缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆