gpt4 book ai didi

c++ - iOS 上的 Google Protocol Buffer

转载 作者:IT老高 更新时间:2023-10-28 21:59:56 25 4
gpt4 key购买 nike

是 iOS 的元语法静态库。 . .

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

。 . .与常规的旧 C++ 编译的原型(prototype)文件兼容吗?我确实想使用生成 Obj-C 的捆绑编译器。

有没有什么方法可以编译谷歌为 iOS 提供的库?

最佳答案

好的。在这种情况下,元句法库(或任何其他第 3 方库)似乎是不必要的。您可以直接将 Google 源代码添加到您的项目中。我在 google 讨论组中找到了 Nicola Ferruzzi 的以下答案。 . .

原来的答案在这里。 . .

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

这个答案的内容包含在下面的图片中,以便永久记录......


编辑

自从今晚第一次再次尝试此操作以来,除了下面概述的步骤之外,我还需要几个步骤(这适用于 protobuf 2.5.0)。

  • 您需要链接到 libz.dylib。您可以在 Build Phases > Link Binary With Libraries 中进行设置。
  • 要轻松删除所有与单元测试相关的内容,请在 google 目录 find . -name "*unittest*" -exec rm -rf {} \; 中的 shell 中使用以下命令
  • 同时删除名为 testing 的文件夹
  • 注释掉#include <google/protobuf/testing/googletest.h>stringprintf.cc
  • 现在请仔细按照以下说明进行操作,一切正常。

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;

}

关于c++ - iOS 上的 Google Protocol Buffer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10277576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com