首页 / 操作系统 / Linux / Protobuf 在Ubuntu 14.04上的编译与使用
摘要:Protobuf 在Ubuntu 14.04上的编译与使用前言一直知道Google开源的一个与语言无关的数据交换协议:protobuf。只知道是一种不同于json和XML的格式,还有就是性能特别的好(这在Java和C++的实现确实是!)最近闲下来看了下Google的Protobuf的相关东西,然而baidu出来的东西很多都过时了,我不得不花些时间来倒腾,于是就有了如下的内容。•下载源代码与准备工作$ sudo apt-get install autoconf automake libtool curl
$ git clone https://github.com/google/protobuf
$ cd protobuf
•修改autogen.sh由于“你懂的”的原因,autogen无法curl下载到gmock的源代码包,所以我把gmock的包放到了自己的github上。修改autogen.sh,让它下载我github上的包peter@ubuntu14:~/protobuf/protobuf$ git diff
diff --git a/autogen.sh b/autogen.sh
index 5b4c29f..f2abf77 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -31,7 +31,7 @@ fi
# directory is set up as an SVN external.
if test ! -e gmock; then
echo "Google Mock not present. Fetching gmock-1.7.0 from the web..."
- curl $curlopts -O https://googlemock.googlecode.com/files/gmock-1.7.0.zip
+ curl $curlopts -L -o gmock-1.7.0.zip https://github.com/peter-wangxu/gMock/archive/1.7.0.zip
unzip -q gmock-1.7.0.zip
rm gmock-1.7.0.zip
mv gmock-1.7.0 gmock#把curl那一行替换成绿色的
•产生configure文件$ ./autogen
•编译与安装protobuf$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig # refresh shared library cache.NOTE: 默认是安装在“/usr/local/lib”下的,在有些平台/usr/local/lib不是默认的LD_LIBRARY_PATH变量里面,可以在通过如下命令改变安装目录$ ./configure --prefix=/usr当看到类似下面的文字,说明protobuf基本安装完成============================================================================
Testsuite summary for Protocol Buffers 3.0.0-beta-2
============================================================================
# TOTAL: 6
# PASS: 6
# SKIP: 0
# XFAIL: 0
# FAIL: 0
# XPASS: 0
# ERROR: 0
============================================================================接下来就是跟Python语言相关的一些配置了
•安装protobuf的Python支持cd python # 位于protobuf下
sudo python setup.py installNOTE: 如果上面命令失败,你可以试试安装下pip的相关包,可以解决些python包的依赖问题sudo apt-get install python-pip--------------------------------------------------------------------------------接下来就是使用protobuf了
•编译.proto文件$ touch DataService.proto
# 放入以下内容message RowProto {
required uint32 null_map = 1;
repeated string column = 2;
}message TableProto {
repeated string column = 1;
repeated string row = 2;
}
•产生py文件,供后面的Python使用protoc --python_out=. ./DataService.proto
•protobuf的使用创建TestDataService.py文件,放入下面内容import sys
import DataService_pb2#create proto
row = DataService_pb2.RowProto()
row.null_map = 1
row.column.append("wang")
row.column.append("female")
row_str=row.SerializeToString()
print "row_str:", row_str
table = DataService_pb2.TableProto()
table.column.append("name")
table.column.append("gender")
table.row.append(row_str)
table_str = table.SerializeToString()#process proto
table_proto = DataService_pb2.TableProto()
table_proto.ParseFromString(table_str)
print "column:"
print table_proto.columnrow_str = table_proto.row[0]
row_proto = DataService_pb2.RowProto()
row_proto.ParseFromString(row_str.encode("utf8"))
print "row1:"
print row_proto.column运行TestDataServer.pypeter@ubuntu14:~/protobuf/proto_test$ python TestDataService.py
row_str: wangfemale
column:
[u"name", u"gender"]
row1:
[u"wang", u"female"]本期的内容就这样了,主要是protobuf的安装与配置,使用涉及的很少,后面有时间会加入更多的使用相关的内容--------------------------------------------------------------------------------FAQ:如果遇到:protoc: error while loading shared libraries: libprotoc.so.10: cannot open shared object file: No such file or directory解决方案sudo ldconfig更多Ubuntu相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-01/127505.htm