Protocol Buffers 学习笔记

1、安装
ubuntu下的安装很简单(虽然要安装几个包):
具体参见:[url]https://github.com/google/protobuf/blob/master/src/README.md[/url]
从github下载了源码后:
$ sudo apt-get install autoconf automake libtool curl make g++ unzip
$ ./autogen.sh
$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig # refresh shared library cache.


[color=red]只编译静态库:[/color]
./configure --disable-shared



2、编译
.proto文件编译:
protoc addressbook.proto --cpp_out .  # 用于生成C++头文件和.cc文件
protoc addressbook.proto --python_out . #用于生成python相应的文件

注意: 可以通过 -I 参数更改搜索路径, 所有文件的路径必须为绝对路径,,
比如官方给的例子(除了上面那种所有文件都在当前文件夹的情况):
protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto

c++文件编译:
下面命令中的pkg-config --cflags --libs protobuf会生成proto buff所需的编译选项(头文件目录,-pthread等)
c++ my_program.cc my_proto.pb.cc `pkg-config --cflags --libs protobuf`


3、教程
python版:[url]https://developers.google.com/protocol-buffers/docs/pythontutorial[/url]
可以print(proto)来看proto实例的内容
C++版:[url]https://developers.google.com/protocol-buffers/docs/cpptutorial[/url]
每个proto类都有一个DebugString()方法,用于返回供人看的内容

完成编译命令:
#只能用g++ 用gcc不行!
echo compile c++ part
protoc addressbook.proto --cpp_out .
g++ writeAddress.cc addressbook.pb.cc -o write.out `pkg-config --cflags --libs protobuf`
g++ readAddress.cc addressbook.pb.cc -o read.out `pkg-config --cflags --libs protobuf`

echo use ./write.out add.txt to generate address file
echo use ./read.out add.txt to read address file

echo compile python part
protoc addressbook.proto --python_out .
echo use python writeAddress.py add.txt to generate address file
echo use python readAddress.py add.txt to read address file


官方教程中有一些错误,,完整的代码(包含编译用的shell命令)见附件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值