Detailed explanation of installing protobuf in linux environment (reproduced)

Reprinted from: http://blog.csdn.net/BeautifulGrils/article/details/52775120


1. Overview of Protocol Buffers:

Protocol Buffers is a lightweight and efficient structured data storage format that can be used for structured data serialization, or serialization. It is very suitable for data storage or RPC data exchange format. A language-independent, platform-independent, and extensible serialized structured data format that can be used in communication protocols, data storage, and other fields. Currently, APIs in C++, Java, and Python are provided.


2. Protocol Buffers installation:



1. Download the installation package of Protocol Buffers:

   

    https://github.com/google/protobuf  

  (Note: The installation package of this article is on github)  

(Or you can use the following link to directly download the installation package of version 2.6.1)

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


2. Installation steps: (You can use xftp to upload the downloaded installation package to linux)


Install the required dependencies:
[root@localhost ~]# yum -y install autoconf automake libtool curl make g++ unzip

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

[root@localhost ~]# cd protobuf-master

generate configure The script file of the file, if this step is not performed, the following operations will not pass

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

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

The installation directory can be modified through ./configure --prefix= command, unified installation under /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 

Go back to the directory where your test.proto file is located and use the command protoc -I=./ --cpp_out=./ to generate the C++ version of the protocol file, and see the .h and .cc files in the current directory

[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

Generate JAVA related protocol files:


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

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


In the current directory, a directory will be generated with the name after the package , and the corresponding java file will be generated


[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 file

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


4. msg.proto file


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;  



Note: In the above example, the package name is lm, and a message helloworld is defined. The message has three members, id of type int32, and another member str of type string. opt is an optional member, that is, the member may not be included in the message.


The above installations are all default installations:

which is:

/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/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325734681&siteId=291194637