这里贴一个介绍贴http://blog.youkuaiyun.com/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;
- }
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;
- }
#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;
- }
#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和.cc
- g++ write.cpp caffe.pb.cc -o write -lprotobuf
- 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>