linux环境下安装protobuf详解(转载)

转自:http://blog.csdn.net/BeautifulGrils/article/details/52775120


一、Protocol Buffers概述:

Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。


二、Protocol Buffers安装:



1、下载Protocol Buffers的安装包:

   

    https://github.com/google/protobuf  

  (注意:本文的安装包在github上下的)  

(或者还可以用以下链接直接下载版本为2.6.1的安装包)

   https://github.com/google/protobuf/archive/v2.6.1.zip


2、安装步骤:(可以用xftp将下好的安装包上传到linux中)


安装需要的依赖包:
[root@localhost ~]# yum -y install autoconf automake libtool curl make g++ unzip

[root@localhost ~]# unzip protobuf-master.zip

[root@localhost ~]# cd protobuf-master

生成configure文件的脚本文件,如果不执行这步,以下操作将通不过

[root@localhost protobuf-master]#  ./autogen.sh 

[root@localhost protobuf-master]#  ./configure

可以修改安装目录通过 ./configure --prefix=命令,统一安装在/usr/local/protobuf下

[root@localhost protobuf-master]# ./configure --prefix=/usr/local/protobuf

 

[root@localhost protobuf-master]#  make


[root@localhost protobuf-master]#  make check


[root@localhost protobuf-master]#  make install

[root@localhost protobuf-master]#  ldconfig # refresh shared library cache.

安装成功

[root@localhost protobuf-master]#  protoc -I=./ --cpp_out=./ test.proto 

回到你的test.proto文件所在目录使用命令protoc -I=./ --cpp_out=./ 生成C++版本的协议文件,在当前目录中看到.h和.cc文件

[root@ilog2 proto]#   ll

-rw-r--r-- 1 root root 18745 Oct 10 11:38 test.pb.cc
-rw-r--r-- 1 root root  9021 Oct 10 11:38 test.pb.h

-rw-r--r-- 1 root root   166 Oct 10 11:35 test.proto

生成JAVA的相关协议文件:


[root@localhost protobuf-master]#  protoc -I=./ --java_out=./ test.proto 

[root@localhost  proto]#  protoc --java_out=./ msg.proto


在当前目录中就会以package的后面的名字生成目录,并生成相应的java文件


[root@localhost  proto]# ll
total 48
drwxr-xr-x 3 root root  4096 Oct 10 11:52 Feinno
drwxr-xr-x 2 root root  4096 Oct 10 11:55 lm
-rw-r--r-- 1 root root   351 Oct 10 11:48 msg.proto
-rw-r--r-- 1 root root 18745 Oct 10 11:38 test.pb.cc
-rw-r--r-- 1 root root  9021 Oct 10 11:38 test.pb.h
-rw-r--r-- 1 root root   166 Oct 10 11:35 test.proto

[root@localhost  proto]# ll lm/
total 28
-rw-r--r-- 1 root root 25966 Oct 10 11:55 Test.java


[root@ilog2 proto]# ll Feinno/Practice/Learn/
total 40
-rw-r--r-- 1 root root 38879 Oct 10 11:52 ProtoBufferPractice.java


3、test.proto文件

   package lm; 
   message helloworld 
   { 
       required int32 id = 1; // ID 
       required string str = 2; // str 
       optional int32 opt = 3; //optional field 
  }


4、msg .proto文件


package Feinno.Practice.Learn;  
  
option java_package = "Feinno.Practice.Learn";  
option java_outer_classname = "ProtoBufferPractice";  
  
message msgInfo  {  
  required int32 ID = 1;  
  required int64 GoodID = 2;         
  required string Url = 3;  
  required string Guid = 4;  
  required string Type = 5;  
  required int32 Order = 6;  



注释:在上例中,package 名字叫做 lm,定义了一个消息 helloworld,该消息有三个成员,类型为 int32 的 id,另一个为类型为 string 的成员 str。opt 是一个可选的成员,即消息中可以不包含该成员。


以上安装都是默认的安装:

即:

/usr/local/bin (可还行的脚本)

/usr/local/include

/usr/local/lib (使用的依赖包)


三、相关好的学习资源:


1、github源码:

   https://github.com/google/protobuf/blob/master/src/README.md

2、一片好的学习资源:

   http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/

猜你喜欢

转载自blog.csdn.net/bodybo/article/details/79036593