谷歌 Google ProtoBuf用法實例

這裏貼一個介紹貼http://blog.csdn.net/hailong0715/article/details/52016682

這裏就介紹怎麼安裝的了,安裝caffe的時候一起安裝的,這裏介紹一下怎麼用這個庫,caffe用他來當數據傳輸說明他很快

新建一個proto定義數據傳輸的結構,這個和ros上數據傳輸很像

  1. syntax = “proto2”;  
  2. package caffe; //域名  
  3. message Person {    
  4.   required string name = 1;    
  5.   required int32 age = 2;    
  6.   optional string email = 3;    
  7.   
  8.   enum PhoneType {    
  9.     MOBILE = 0;    
  10.     HOME = 1;    
  11.     WORK = 2;    
  12.   }    
  13.     
  14.   message PhoneNumber {    
  15.     required string number = 1;    
  16.     optional PhoneType type = 2 [default = HOME];    
  17.   }    
  18.     
  19.   optional PhoneNumber phone = 4;   
  20.     
  21. }    
syntax = "proto2";
package caffe; //域名
message Person {  
  required string name = 1;  
  required int32 age = 2;  
  optional string email = 3;  

  enum PhoneType {  
    MOBILE = 0;  
    HOME = 1;  
    WORK = 2;  
  }  

  message PhoneNumber {  
    required string number = 1;  
    optional PhoneType type = 2 [default = HOME];  
  }  

  optional PhoneNumber phone = 4; 

}  

package 指的是域名,就是std一樣

write.cpp

  1. #include <iostream>    
  2. #include <fstream>    
  3. #include “caffe.pb.h”    
  4.     
  5. using namespace std;    
  6. using namespace caffe;    
  7.     
  8. int main()    
  9. {    
  10.     Person person;    
  11.     
  12.     person.set_name(”flamingo”);       
  13.     person.set_age(18);     
  14.     person.set_email([email protected]);    
  15.     person.mutable_phone()->set_number(”135525”);  
  16.       
  17.     // Write    
  18.     fstream output(”./log”, ios::out | ios::trunc | ios::binary);    
  19.     
  20.     if (!person.SerializeToOstream(&output)) {    
  21.         cerr << ”Failed to write msg.” << endl;    
  22.         return -1;    
  23.     }    
  24.     
  25.     //system(“pause”);    
  26.     return 0;    
  27. }    
#include <iostream>  




include <fstream>

include "caffe.pb.h"

using namespace std;
using namespace caffe;

int main()
{
Person person;

person.set_name("flamingo");     
person.set_age(18);   
person.set_email("[email protected]");  
person.mutable_phone()-&gt;set_number("135525");

// Write  
fstream output("./log", ios::out | ios::trunc | ios::binary);  

if (!person.SerializeToOstream(&amp;output)) {  
    cerr &lt;&lt; "Failed to write msg." &lt;&lt; endl;  
    return -1;  
}  

//system("pause");  
return 0;  
}

上面的修改其實我是看他生產的caffe.pb.h裏面定義的函數,發現有這些函數可以直接用的

read.cpp

  1. #include <iostream>    
  2. #include <fstream>    
  3. #include “caffe.pb.h”    
  4.     
  5.   
  6. using namespace std;    
  7. using namespace caffe;    
  8.     
  9. void PrintInfo(const Person & person) {     
  10.     cout << person.name() << endl;     
  11.     cout << person.age() << endl;     
  12.     cout << person.email() << endl;    
  13.     cout << person.phone().number() << endl;   
  14.     cout << person.phone().type() << endl;   
  15.   
  16. }     
  17.     
  18. int main()    
  19. {    
  20.     Person person;      
  21.     
  22.     fstream input(”./log”, ios::in | ios::binary);    
  23.         
  24.     if (!person.ParseFromIstream(&input)) {    
  25.         cerr << ”Failed to parse address book.” << endl;    
  26.         return -1;    
  27.     }    
  28.     
  29.     PrintInfo(person);    
  30.   
  31.     return 0;    
  32. }  
#include <iostream>  




#include <fstream> #include "caffe.pb.h" using namespace std; using namespace caffe; void PrintInfo(const Person & person) { cout << person.name() << endl; cout << person.age() << endl; cout << person.email() << endl; cout << person.phone().number() << endl; cout << person.phone().type() << endl; } int main() { Person person; fstream input("./log", ios::in | ios::binary); if (!person.ParseFromIstream(&input)) { cerr << "Failed to parse address book." << endl; return -1; } PrintInfo(person); return 0; }

然後編譯,加入庫就好了

  1. protoc –cpp_out=./ caffe.proto   #在當前目錄生成.h和.cc  
  2. g++ write.cpp caffe.pb.cc -o write -lprotobuf  
  3. g++ read.cpp caffe.pb.cc -o read -lprotobuf  
protoc --cpp_out=./ caffe.proto   #在當前目錄生成.h和.cc
g++ write.cpp caffe.pb.cc -o write -lprotobuf
g++ read.cpp caffe.pb.cc -o read -lprotobuf




            </div>
                </div>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章