這裡貼一個介紹貼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