歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Protobuf 在Ubuntu 14.04上的編譯與使用

Protobuf 在Ubuntu 14.04上的編譯與使用

日期:2017/3/1 9:19:26   编辑:Linux編程

摘要: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

更多Ubuntu相關信息見Ubuntu 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=2

Copyright © Linux教程網 All Rights Reserved