Protobuf 在Ubuntu 14上的編譯與使用
前言
一直知道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
</ul>
由于“你懂的”的原因,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 @@ fidirectory 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</pre>
#把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 ============================================================================</pre>
接下來就是跟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; }</pre>
- 產生py文件,供后面的Python使用
</ul>
protoc --python_out=. ./DataService.proto
- protobuf的使用
</ul>
創建TestDataService.py文件,放入下面內容
import sys import DataService_pb2create 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</pre>
運行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參考文章:
https://github.com/google/protobuf
http://blog.csdn.net/whuqin/article/details/8730026