Google protocol buffer使用筆記__Google

來源:互聯網
上載者:User
一 下載
Google下載地址:https://developers.google.com/protocol-buffers/docs/downloads?hl=zh-CN
Github下載地址:https://github.com/google/protobuf
我這裡下載版本:protobuf-2.6.1.tar.gz


二 編譯
1 解壓
將上面的壓縮包解壓到檔案夾 protobuf-2.6.1 中。


2 編譯
2.1 在protobuf-2.6.1中,找到vsprojects/protobuf.sln,雙擊用vs開啟。
2.2 設定編譯模式為Release模式,依次編譯libprotobuf,libprotobuf-lite,libprotoc,protoc(右鍵->產生)。


3 拷貝庫
在vsprojects\Release找到libprotobuf.lib,libprotobuf-lite.lib,libprotoc.lib和protoc.exe,拷貝到c:\windows目錄。


4 產生標頭檔
雙擊extract_includes.bat,就會在vsprojects目錄下產生一個include目錄。


三 編譯proto
1 在D:/gpb目錄下建立person.proto,編輯內容如下:
package proto;message Person {  required string name = 1;  required int32 id = 2;  optional string email = 3;}
#gpb即Google protocol buffer


2 產生C++類
在gpb檔案中,右鍵->在此處開啟命令列,輸入:
protoc.exe person.proto --cpp_out=.


這樣就在當前檔案夾下面產生了person類,檔案如下:



四 proto詳解
1 關鍵字
package關鍵字是指定命名空間
message關鍵字,類似C中的struct或者C++和Java中的class


2 屬性說明
required非空欄位
optional可選欄位
repeated表示的欄位可以包含0個或多個資料。


3 類型

如圖:



原文連結:http://blog.csdn.net/xufeng0991/article/details/45031211

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.