谷歌 Google ProtoBuf用法实例
2017-05-05 16:37
399 查看
这里贴一个介绍贴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和.cc
g++ write.cpp caffe.pb.cc -o write -lprotobuf
g++ read.cpp caffe.pb.cc -o read -lprotobuf
这里就介绍怎么安装的了,安装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和.cc
g++ write.cpp caffe.pb.cc -o write -lprotobuf
g++ read.cpp caffe.pb.cc -o read -lprotobuf
相关文章推荐
- 谷歌 Google ProtoBuf用法实例
- 谷歌 Google ProtoBuf用法实例
- 高性能RPC over MINA&google protobuf 代码&实例 (二)
- google protobuf 简单实例
- 高性能RPC over MINA&google protobuf 代码&实例 (一)
- google protobuf 简单实例
- 高性能RPC over MINA&google protobuf 代码&实例 (二)
- google的protobuf用法
- google protobuf 简单实例
- 高性能RPC over MINA&google protobuf 代码&实例 (一)
- 高性能RPC over MINA&google protobuf 代码&实例 (一)
- google ProtoBuf开发者指南
- 一种自动反射消息类型的 Google Protobuf 网络传输方案
- google protobuf repeated 字段二义性
- Google Protobuf 使用介绍
- Google ProtoBuf 支持N+1种语言
- Google protobuf - protoc-gen-as3
- 一种自动反射消息类型的 Google Protobuf 网络传输方案
- google ProtoBuf开发者指南
- google ProtoBuf 简介入门