Protobuf 在Ubuntu 14.04上的编译与使用
来源:互联网 发布:移动数据被禁用怎么开 编辑:程序博客网 时间:2024/06/05 01:13
摘要: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 install
NOTE: 如果上面命令失败,你可以试试安装下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.column
row_str = table_proto.row[0]
row_proto = DataService_pb2.RowProto()
row_proto.ParseFromString(row_str.encode('utf8'))
print "row1:"
print row_proto.column
运行TestDataServer.py
peter@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
- Protobuf 在Ubuntu 14.04上的编译与使用
- Protobuf在ios上的使用
- google protobuf在ubuntu上安装使用步骤
- kaldi在ubuntu上的编译与安装
- 在lua环境中使用protobuf ,编译protobuf文件
- 找出ubuntu上所有安装的protobuf
- CLAPACK在Windows上的编译、安装与使用
- 在xcode上使用zmq和protobuf
- 在ubuntu上编译android的mupdf
- vlc在ubuntu上的编译
- ubuntu下编译protobuf
- ubuntu下编译protobuf
- ubuntu编译google protobuf
- ubuntu下编译protobuf
- ProtoBuf Ubuntu 中编译
- ubuntu编译protobuf库
- sopcast在ubuntu上的安装与使用
- NFS在ubuntu上的安装与使用
- 第一次 博客 我决定写的 认(fu)真(yan)点
- 十 项目1(3)
- [DeeplearningAI笔记]ML strategy_2_2训练和开发/测试数据集不匹配问题
- 顺序容器操作
- 数字三角形
- Protobuf 在Ubuntu 14.04上的编译与使用
- RxAndroid线程调度器(五)
- C语言快速排序算法及三种优化方式
- 简单计算器
- POJ 2001 Shortest Prefixes
- Java并发编程:如何创建线程?
- Android Service生命周期,本地服务远程服务,前台服务后台服务,启动方式,与IntentService区别全面解析
- eclipse中jd反编译安装
- tensorflow学习二