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

由于“你懂的”的原因,autogen无法curl下载到gmock的源代码包,所以我把gmock的包放到了自己的github上。修改autogen.sh,让它下载我github上的包

[email protected]:~/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

[email protected]:~/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

时间: 2024-08-11 03:38:22

Protobuf 在Ubuntu 14上的编译与使用的相关文章

Ubuntu 14.02下编译及配置apache2.4(python CGI)

Ubuntu 14.02下编译及配置apache2.4(python CGI): # 下载apache2.4源码包 wget http://mirrors.cnnic.cn/apache//httpd/httpd-2.4.12.tar.gz # 安装apr依赖库 apt-get install libapr1 libaprutil1 libapr1-dev libaprutil1-dev # 解压并编译安装 tar -xvf httpd-2.4.12.tar.gz cd httpd-2.4.12

【转】基于Ubuntu 14.04 LTS编译Android4.4.2源代码

原文网址:http://blog.csdn.net/gobitan/article/details/24367439 基于Ubuntu 14.04 LTS编译Android4.4.2源代码 Dennis Hu 2014-4-21 环境准备: 基本环境:ubuntu-14.04-desktop-64bit LTS(裸机或者Windows下虚拟机安装均可,12.04也可以,但10.04目前不支持了) 其他要求:空闲磁盘空间100G以上,代码部分接近10G,其他为git和编译中间和目标文件准备 ===

基于Ubuntu 14.04 LTS编译Android4.4.2源代码

转载自:雨水:http://blog.csdn.net/gobitan/article/details/24367439 基于Ubuntu 14.04 LTS编译Android4.4.2源代码 Dennis Hu 2014-4-21 环境准备: 基本环境:ubuntu-14.04-desktop-64bit LTS(裸机或者Windows下虚拟机安装均可,12.04也可以,但10.04目前不支持了) 其他要求:空闲磁盘空间100G以上,代码部分接近10G,其他为git和编译中间和目标文件准备 =

VoIP应用在Ubuntu 14.04下编译FFmpeg libX264及PJSIP

PJSIP是一个开源的SIP协议栈.它支持多种SIP的扩展功能,可说算是最目前流行的SIP协议栈之一了.  它实现了SIP.SDP.RTP.STUN.TURN和ICE.PJSIP作为基于SIP的一个多媒体通信框架提供了非常清晰的API,以及NAT穿越的功能.PJSIP具有非常好的移植性,几乎支持现今所有系统:从桌面系统.嵌入式系统到智能手机.PJSIP同时支持语音.视频.状态呈现和即时通讯. PJSIP很强大,很多VOIP语音视频通话应用都用它… 下面来说说如何在ubuntu下进行编译 文章目录

[Linux 小技巧] Ubuntu 14.04 下编译、安装、配置最新开发版 GoldenDict

1. 背景介绍 GoldenDict 是一款非常优秀的跨平台电子词典软件,支持 StarDict.Babylon 等多种词典.其 PC 版基于 Qt 编写,完全免费.开源且没有广告.GoldenDict 的 Android 版是另外一个全新实现的系统,采用了付费版去广告.免费版显示广告的策略,这里就不详细讨论了. GoldenDict 在 GitHub 上的地址是 https://github.com/goldendict/goldendict,截至目前(2015-10-17)已经有 1872

ubuntu 14.10 android 编译开发环境搭建

1.替换一个中文输入法.比如搜狗:sogou_pinyin_linux_1.1.0.0037_amd64.deb 下载网址:http://pinyin.sogou.com/linux/ 2.下载vim编辑器, 可以使用sudo apt-get install vim  链接到网络进行下载. 使用apt-get install vim 之前需要配置apt-get /etc/apt/sources.list文件. 1.备份一下你本地的/etc/apt/sources.list.可以使用命令 sudo

在Ubuntu 14 上安装 Nginx-RTMP 流媒体服务器

一:RTMP RTMP流媒体协议是 一套 Adobe 开发的音频视频实时传输协议: 二:Nginx-rtmp nginx-rtmp 是一个基于nginx的 RTMP服务模块,开源,免费 https://github.com/arut/nginx-rtmp-module 三:在 ubuntu server 14 安装流程 1.先下载安装  nginx 和 nginx-rtmp 编译依赖工具 sudo apt-get install build-essential libpcre3 libpcre3

ubuntu 14.04下编译shared_ptr失败

1.错误信息如下 #include <iostream> #include <memory> #include <boost/shared_ptr.hpp> using namespace std; using namespace boost; int main(void) { for (int i = 0; i < 10000; ++i) { //unique_ptr<int> int_ptr(new int()); shared_ptr<in

Hadoop 2.30 在Ubuntu 14.04 中编译

转载请注明作者:KiwenLau,以及原文地址:http://www.cnblogs.com/kiwenlau/p/4227204.html Hadoop官网提供的编译好的hadoop-2.3.0.tar.gz二进制包是在32位系统上编译的,在64系统上运行会有一些错误,比如: WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java class