Google Protobuf - 實現跨平臺跨語言的序列化/反序列化

fmms 12年前發布 | 27K 次閱讀 Google

0 Overview

Google Protocol Buffer 是一個平臺無關、語言無關的結構化數據的序列化與反序列化工具。

1 Establish dev environment

wget http://protobuf.googlecode.com/files/protobuf-2.4.1.tar.gz
tar zxvf protobuf-2.4.1.tar.gz
cd protobuf-2.4.1
mkdir /Users/michael/Development/opt/protobuf-2.4.1
./configure --prefix=/Users/michael/Development/opt/protobuf-2.4.1
make
make check
make install

2 Protocol file

創建一個名為 lm.helloworld.proto 的文件。

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

一般 Google Protobuf 的 protocol 文件名形如:

packageName.MessageName.proto

3 Generate protocol classes

此時的目錄結構:

protobuf-2.4.1
|---bin
|---include
|---lib
|---test
    |---lm.helloworld.proto

編譯生成 Google Protobuf 類文件:

alias protoc='/Users/michael/Development/opt/protobuf-2.4.1/bin/protoc'
SRC_DIR=.
DST_DIR=.
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/lm.helloworld.proto

此時的目錄結構為:

protobuf-2.4.1
|---bin
|---include
|---lib
|---test
    |---lm.helloworld.proto
    |---lm.helloworld.pb.cc
    |---lm.helloworld.pb.h

4 Application files

此處以 File Stream 為例。

4.1 Structured data -> Stream

#include "lm.helloworld.pb.h"
#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
    lm::helloworld msg1;
    msg1.set_id(101);
    msg1.set_str("hello");

    fstream output("./log", ios::out | ios::trunc | ios::binary);

    if (!msg1.SerializeToOstream(&output))
    {
        cerr << "Failed to write msg." << endl;
        return -1;
    }
    return 0;
}

跟已有的結構化數據結構(依據 Google Protobuf 的格式)創建數據,將結構化數據序列化到流中。

4.2 Stream -> Structured data

#include "lm.helloworld.pb.h"                                                                                                                                                                             
#include <iostream>
#include <fstream>

using namespace std;

void ListMsg(const lm::helloworld & msg)
{
    cout << msg.id() << endl; 
    cout << msg.str() << endl; 
} 

int main(int argc, char* argv[])
{ 
    lm::helloworld msg1;
    {   
        fstream input("./log", ios::in | ios::binary);
        if (!msg1.ParseFromIstream(&input))
        {   
            cerr << "Failed to parse address book." << endl;
            return -1; 
        }   
    }   
    ListMsg(msg1);
}

將流中的序列化數據,讀取到依據 Google Protobuf 的格式創建的對象中。

5 Compile executable files

5.1 Directories and file tree

protobuf-2.4.1
|---bin
|---include
|---lib
|---test
    |---lm.helloworld.proto
    |---lm.helloworld.pb.cc
    |---lm.helloworld.ph.h
    |---write.cpp
    |---read.cpp

5.2 Compile

g++ lm.helloworld.pb.cc write.cpp -o write.out -I ../include -L../lib -lprotobuf

Notice:

  • 編譯應用程序源文件時,要記得同時編譯 lm.helloworld.pb.cc 源文件;
  • 記得 Include Google Protobuf headers(-I ../include)
  • 記得 Link 路徑以及相應的 google protobuf libraries(-L../lib -lprotobuf)

6 Run

運行 write:

./write

會觀察到生成如下文件(參見源程序):

log

運行 read:

./read

輸出結果:

$ ./read.out 
101
hello

7 Review

  1. 環境:搭建 Google Protobuf 的開發環境;
  2. 協議:根據 Google Protobuf 格式要求,創建 Protocol 文件;
  3. 生成:利用 protoc 編譯生成所定義的 Protocol 的類源文件與頭文件;
  4. 應用:編寫利用所生成的 Protocol 的類源文件與頭文件;
  5. 編譯:編譯所編寫的應用的源文件與頭文件,注意頭文件路徑、庫文件路徑及庫;
  6. 運行。

Reference

  1. http://stackoverflow.com/questions/8875867/linking-errors-when-using-proto-tutorial
  2. http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/
  3. http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/overview.html

-

Happy Coding, enjoy sharing!

轉載請注明來自“柳大的CSDN博客”:Blog.CSDN.net/Poechant

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!