前言

一直知道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
--- a/autogen.sh
+++ b/autogen.sh
@@ -, +, @@ 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..zip
rm gmock-1.7..zip
mv gmock-1.7. 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:
# PASS:
# SKIP:
# XFAIL:
# FAIL:
# XPASS:
# ERROR:
============================================================================

接下来就是跟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 = ;
repeated string column = ;
} message TableProto {
repeated string column = ;
repeated string row = ;
}
  • 产生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.: 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

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

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

    原文网址:http://blog.csdn.net/gobitan/article/details/24367439 基于Ubuntu 14.04 LTS编译Android4.4.2源代码       ...

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

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/gobitan/article/details/24367439 基于Ubuntu 14.04 LTS ...

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

    转载自:雨水:http://blog.csdn.net/gobitan/article/details/24367439 基于Ubuntu 14.04 LTS编译Android4.4.2源代码     ...

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

    PJSIP是一个开源的SIP协议栈.它支持多种SIP的扩展功能,可说算是最目前流行的SIP协议栈之一了.  它实现了SIP.SDP.RTP.STUN.TURN和ICE.PJSIP作为基于SIP的一个多 ...

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

    1. 背景介绍 GoldenDict 是一款非常优秀的跨平台电子词典软件,支持 StarDict.Babylon 等多种词典.其 PC 版基于 Qt 编写,完全免费.开源且没有广告.GoldenDic ...

  6. ubuntu 14.04.5 编译Android 4.4.4 r1源码(最新)

    本文博客链接:http://blog.csdn.net/qq1084283172/article/details/54426189 吐槽:ubuntu系统真是让人又爱又恨,也有可能是VMware Wo ...

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

    一:RTMP RTMP流媒体协议是 一套 Adobe 开发的音频视频实时传输协议: 二:Nginx-rtmp nginx-rtmp 是一个基于nginx的 RTMP服务模块,开源,免费 https:/ ...

  8. Ubuntu 14.10 下编译Hadoop2.4.0

    在http://www.aboutyun.com/thread-8130-1-1.html 这里看到到,安装过程遇到了上面说的问题,所以将此文转载过来,以备不时之需,感谢此作者. 问题导读: 1.如果 ...

  9. ubuntu 14上安装mysql离线包

    1.下载mysql在linux下离线安装包文件:wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.33-linux-glibc2. ...

随机推荐

  1. Valgrind使用记录

    0.安装valgrind wget http://valgrind.org/downloads/valgrind-3.11.0.tar.bz2 tar xvf valgrind-3.11.0.tar. ...

  2. AngularJS的五个超酷特性

    AngularJS是一个超棒的javascript框架,不单单对于开发人员来说非常有吸引力,对于UI设计师来说也同样出色.在这篇教程中,我们将简单的介绍AngularJS几个重量级必备特性,并且介绍它 ...

  3. Linux中的zero-copy技术

    本文是对参考文章重要部分的摘录,同时进行简单地总结. 零拷贝技术的相关背景知识 传统的 Linux 操作系统的标准 I/O 接口是基于数据拷贝操作的,即 I/O 操作会导致数据在操作系统内核地址空间的 ...

  4. jquery常用的一些方法

    一.选择网页元素(标签选择器) $(document) //选择整个文档对象 $('#myId') //选择ID为myId的网页元素 $('div.myClass') // 选择class为myCla ...

  5. Redis 提供的好的解决方案 实例

    1.Redis 支持的String 类型的,它是 二进制的,比较安全,当有一些 图片或者css文件影响运行速度的时候,可以缓存之.

  6. UE4里的渲染线程

    记的上次看过UniRx里的源代码,说是参考微软的响应式编程框架,响应式编程里的一些理论不细说,只单说UniRx里的事件流里的事件压入与执行,与UE4的渲染线程设计有很多相同之处,如果有了解响应式编程相 ...

  7. [CSS3] 学习笔记-HTML与CSS简单页面效果实例

    一个简单的首页的设计: html文件: <!doctype html> <html> <head> <meta charset="UTF-8&quo ...

  8. 穷举 迭代 while

    for循环拥有两类: 穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 例:1.单位给发了一张150元购物卡,拿着到超市买三类洗化用品.洗发水15元,香皂2元,牙刷5元.求刚好花 ...

  9. 蓝桥网试题 java 基础练习 字母图形

    ----------------------------------------------------------------- 不知道说啥 感觉好像偏离主体思想了 但是这样写好简单 ------- ...

  10. 《浅谈架构之路:单点登录 SSO》

    前言:SSO 单点登录 “半吊子”的全栈工程师又来了,技术类的文章才发表了两篇,本来想先将主攻的几个系列都开个头(Nodejs.Java.前端.架构.全栈等等),无奈博客起步太晚,写博文的时间又没有很 ...