ProtocolBuffer for Objective-C 運行環境配置(真正測試過的)

一、下載ProtocolBuffer包(2.5版本與下面的object-c插件不兼容,建議下載2.4.1版本的):


             http://code.google.com/intl/zh-CN/apis/protocolbuffers/

ios

     編譯安裝(生成的編譯器protoc在/usr/local/bin目錄下):c++


           cd protobuf-2.4.1git

           ./configure
           make
           make install
github


二、下載Objective-C compiler for ProtocolBuffer(目前有兩種類型的實現)。api


    (1)、針對ProtocolBuffer2.2作修改,使最後生成的.proto文件編譯器(protoc)支持Objective-C類型的文件輸出。

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

    (2)、針對ProtocolBuffer2.3推出的plugin模式編寫插件,以插件方式支持Objective-C類型的文件輸出。

                       https://github.com/booyah/protobuf-objc

        我選用第(1)種方式,這也是Google推薦的方式。

                        http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers,下載源碼壓縮包後解壓到相關目錄

        進入該目錄,並執行:
app


                       cd  ProtocolBuffers-2.2.0-Source
測試

                       ./configure
                       make
                       make install
ui

沒有這麼順利google


google/protobuf/message.cc:130:60: error: implicit instantiation of undefined
      template 'std::__1::basic_istream<char, std::__1::char_traits<char> >'
  return ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
                                                           ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:109:33: note: 
      template is declared here
    class _LIBCPP_TYPE_VIS_ONLY basic_istream;
                                ^
google/protobuf/message.cc:135:67: error: implicit instantiation of undefined
      template 'std::__1::basic_istream<char, std::__1::char_traits<char> >'
  return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof();
                                                                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:109:33: note: 
      template is declared here
    class _LIBCPP_TYPE_VIS_ONLY basic_istream;
                                ^
2 warnings and 2 errors generated.
make[2]: *** [message.lo] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

atom

  • 解決方法以下:

找到文件src/google/protobuf/message.cc     在文件的第一個#include之上的一行,加入 

#include <istream>

繼續運行make ,即成功經過。



三、測試.proto文件編譯


        寫一個person.proto文件:

[cpp] view plaincopy

  1. message Person {  

  2.  required string name = 1;  

  3.  required int32 id = 2;  

  4.  optional string email = 3;  

  5.    

  6.  enum PhoneType {  

  7.     MOBILE = 0;  

  8.     HOME = 1;  

  9.     WORK = 2;  

  10.  }  

  11.    

  12.  message PhoneNumber {  

  13.     required string number = 1;  

  14.     optional PhoneType type = 2 [default = HOME];  

  15.  }  

  16.    

  17.  repeated PhoneNumber phone = 4;  

  18. }  



        編譯該文件:

                protoc  person.proto   --objc_out=/Output/Directory/   


        protoc會使用該插件編譯.proto文件,最終生成兩個文件:Person.pb.h 、Person.pb.m(不支持ARC


        若是工程中使用了ARC ,因此須要使用-fno-objc-arc來標識相關的文件不使用ARC機制:



        這個步驟經過後,說明ProtocoBuffer Compiler for Objective-C能夠正常工做了。

四、在Xcode中使用ProtocolBuffer


        將步驟2中protobuf-obj/src/runtime/Classes目錄導入到Xcode項目中,導入時,選中」Copy items into destination group‘s folder(if needed)「。

        導入位置選擇項目根目錄。導入完畢後,項目根目錄下將會出現Classes目錄,將該目錄更名爲ProtocolBuffers(注意最後的s): mv Classes ProtocolBuffers

        修改項目屬性中」Build Settings-->Search Paths-->Header Search Paths」,將項目根目錄「.」添加到頭文件搜索路徑中去。

        這樣ProtocolBuffer for Objective-C的工做環境就配置好了。


五、使用


        1)、將步驟3中編譯輸出的Person.pb.h 和Person.pb.m添加到項目中


        2)、將Person.pb.h 中的 #import <ProtocolBuffers/ProtocolBuffers.h> 改成#import"ProtocolBuffers/ProtocolBuffers.h"


        3)、在須要使用的地方引入頭文件:#import "Person.pb.h"

[cpp] view plaincopy

  1. - (void)viewDidLoad  

  2. {  

  3.     [super viewDidLoad];  

  4.     // Do any additional setup after loading the view, typically from a nib.  

  5.       

  6.     [self writeAndReadProtobuf];  

  7. }  

  8.   

  9. - (void)writeAndReadProtobuf{  

  10.     Person *person = [[[[[Person builder] setName:@"極致"]  

  11.                         setId:1]  

  12.                        setEmail:@"abc@163.com"] build];  

  13.     NSData *data = [person data];  

  14.       

  15.     NSString *docPath = [self applicationDocumentsDirectory];  

  16.     NSString *path = [docPath stringByAppendingFormat:@"/person.data"];  

  17.       

  18.     if ([data writeToFile:path atomically:YES]) {  

  19.         [self readFileWithPath:path];  

  20.     }  

  21. }  

  22.   

  23. - (NSString *)applicationDocumentsDirectory {  

  24.       

  25.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  

  26.     NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;  

  27.     return basePath;  

  28. }  

  29.   

  30. - (void)readFileWithPath:(NSString *)path {  

  31.       

  32.     NSData *data = [NSData dataWithContentsOfFile:path];  

  33.     Person *person = [Person parseFromData:data];  

  34.       

  35.     if (person) {  

  36.         NSLog(@"\n id %d \n name: %@ \n email: %@ \n",person.id, person.name, person.email);  

  37.     }  

  38. }  

輸出打印的結果以下:

              

遇到的問題:


          在執行 ./autogen.sh時出現錯誤:   
./autogen.sh: line 10: autoreconf: command not found

          

          解決辦法:須要安裝automake和autoconf:


                      brew install automake

                      brew install autoconf 

相關文章
相關標籤/搜索