Google Google ProtoBuf用法執行個體

來源:互聯網
上載者:User

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

這裡就介紹怎麼安裝的了,安裝caffe的時候一起安裝的,這裡介紹一下怎麼用這個庫,caffe用他來當資料轉送說明他很快

建立一個proto定義資料轉送的結構,這個和ros上資料轉送很像

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

#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("majianfei1023@gmail.com");      person.mutable_phone()->set_number("135525");        // Write      fstream output("./log", ios::out | ios::trunc | ios::binary);        if (!person.SerializeToOstream(&output)) {          cerr << "Failed to write msg." << endl;          return -1;      }        //system("pause");      return 0;  }  

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

read.cpp

#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;  }


然後編譯,加入庫就好了

protoc --cpp_out=./ caffe.proto   #在目前的目錄產生.h和.ccg++ write.cpp caffe.pb.cc -o write -lprotobufg++ read.cpp caffe.pb.cc -o read -lprotobuf




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.