protobuf 安装与使用
来源:互联网 发布:淘宝店类目可以改吗 编辑:程序博客网 时间:2024/05/16 08:39
安装
安装依赖
autoconf
sudo apt-get install autoconf
libtool
sudo apt-get install libtool
准备源码
- 下载源码 protobuf 2.6.1
- 解压 unzip protobuf-2.6.1.zip
- 下载gtest googletest-release-1.5.0.zip
- unzip googletest-release-1.5.0.zip ; mv googletest-release-1.5.0 protobuf-2.6.1/gtest
安装
$ ./autogen.sh
$ ./configure
$ make
$ make check
$ make install
使用
定义协议
创建person.proto
message Person
{
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
编译协议
$ protoc person.proto --cpp_out=.
$ ls
person.pb.cc person.pb.h person.proto
编码数据
#include "person.pb.h"
#include <fstream>
using std::ofstream;
int main()
{
Person person;
person.set_name("spch2008");
person.set_id(12);
person.set_email("spch2008@foxmail.com");
ofstream fout("pb_info.dat", std::ios::out | std::ios::binary);
person.SerializeToOstream(&fout);
return 0;
}
$ g++ person.pb.cc write.cc -o writer -lprotobuf
$ ./writer
数据解码
#include <iostream>
#include <fstream>
#include "person.pb.h"
using namespace std;
int main()
{
ifstream fin("pb_info.dat", std::ios::in | std::ios::binary);
Person person;
person.ParseFromIstream(&fin);
cout << "name: " << person.name() << endl;
cout << "email: " << person.email() << endl;
return 0;
}
$ g++ person.pb.cc read.cc -o reader -lprotobuf
$ ./reader
name: spch2008
email: spch2008@foxmail.com
疑难
protoc: error while loading shared libraries: libprotoc.so.9: cannot open shared object file: No such file or directory
export LD_LIBRARY_PATH=/usr/local/lib
0 0
- protobuf安装与使用
- protobuf 安装与使用
- protobuf 安装与使用
- protobuf 的安装与使用
- protobuf for python 安装与使用
- protobuf安装与示例
- protobuf 安装和使用
- protobuf安装使用
- Protobuf windows安装使用
- google protobuf 在Linux下安装与使用
- protobuf源码编译与安装
- Protobuf 的安装使用总结
- protobuf的安装和使用
- protobuf的安装和使用
- mac protobuf java安装使用
- ProtoBuf安装和使用简介
- Protobuf-Net 下载安装使用
- protobuf + vs2013 下载.安装与编译!
- C#控制台 派生类使用new屏蔽基类的字段与方法
- HBase伪分布式安装的简单步骤记录
- 使用jquery获取父元素或父节点的方法
- 正则表达式
- Android Launcher开发添加应用程序的快捷方式
- protobuf 安装与使用
- React Native 常见问题总结(android端)
- 修改tabbar,navbar高度
- Codeforecs 550D Regular Bridge (神奇的构造题)
- Android 使用ViewPager实现左右循环滑动图片
- React升级后带来的两个小问题及处理
- ScrollView 非手动调用的方法
- git服务器ubuntu下操作使用说明
- makefile 浅析(一)