【Protocol Buffer】Protocol Buffer入门教程(一):简介和安装
00. 目錄
文章目錄
- 00. 目錄
- 01. Protocol Buffer簡介
- 02. Protocol Buffer優缺點
- 03. Protocol Buffer安裝
- 04. Protocol Buffer測試
- 05. 參考
代碼下載:01_demo.rar
01. Protocol Buffer簡介
protobuf也叫protocol buffer是google 的一種數據交換的格式,它獨立于語言,獨立于平臺。google 提供了多種語言的實現:java、c#、c++、go 和 python,每一種實現都包含了相應語言的編譯器以及庫文件。
由于它是一種二進制的格式,比使用 xml 、json進行數據交換快許多。可以把它用于分布式應用之間的數據通信或者異構環境下的數據交換。作為一種效率和兼容性都很優秀的二進制數據傳輸格式,可以用于諸如網絡傳輸、配置文件、數據存儲等諸多領域。
02. Protocol Buffer優缺點
優點:
- 性能好/效率高
- 代碼生成機制
- 支持“向后兼容”和“向前兼容”
- 支持多種編程語言
缺點:
- 應用不夠廣(相比xml和json)
- 二進制格式導致可讀性差
- 缺乏自描述
03. Protocol Buffer安裝
源碼包下載:Protocol Buffer源碼包下載
源碼包下載:protobuf-master(20191007).zip
源碼包下載:protobuf-cpp-3.9.2.tar.gz
安裝環境:Ubuntu 18.04
3.0 安裝所需工具
deng@itcast:~/protobuf-master$ sudo apt-get install autoconf automake libtool curl make g++ unzip3.1 解壓源代碼
deng@itcast:~$ unzip protobuf-master.zip3.2 進入protobuf-master目錄
deng@itcast:~$ cd protobuf-master/ deng@itcast:~/protobuf-master$3.3 自動生成configure配置文件
deng@itcast:~/protobuf-master$ pwd /home/deng/protobuf-master deng@itcast:~/protobuf-master$ ./autogen.sh3.4 生成Makefile
deng@itcast:~/protobuf-master$ ./configure3.5 編譯源代碼
deng@itcast:~/protobuf-master$ make -j43.6 安裝
deng@itcast:~/protobuf-master$ sudo make install3.7 刷新動態庫
deng@itcast:~/protobuf-master$ sudo ldconfig deng@itcast:~/protobuf-master$溫馨提示:
源碼包中的src/README.md, 有詳細的安裝說明。
04. Protocol Buffer測試
4.1 測試examples中程序
在源代碼包中,有個examples文件夾,把里面的addressbook.proto、add_person.cc、list_people.cc拷貝出來。
deng@itcast:~/protobuf-master/examples$ mkdir ~/test01 deng@itcast:~/protobuf-master/examples$ cp add_person.cc list_people.cc addressbook.proto ~/test01/ deng@itcast:~/protobuf-master/examples$4.2 編譯 .proto文件
deng@itcast:~/test01$ ls add_person.cc addressbook.proto list_people.cc deng@itcast:~/test01$ protoc addressbook.proto --cpp_out=./ deng@itcast:~/test01$ ls add_person.cc addressbook.pb.h list_people.cc addressbook.pb.cc addressbook.proto deng@itcast:~/test01$說明:
- protoc:protobuf自帶的編譯工具,將.proto文件生成指定的類
- –cpp_out:指定輸出特定的語言和路徑
? 通過protoc工具編譯.proto文件時,編譯器將生成所選擇語言的代碼,這些代碼可以操作在.proto文件中定義的消息類型,包括獲取、設置字段值,將消息序列化到一個輸出流中,以及從一個輸入流中解析消息。
? 對C++來說,編譯器會為每個.proto文件生成一個.h文件和一個.cc文件,.proto文件中的每一個消息有一個對應的類。
4.3 寫文件(序列化)測試
如果沒有pkg-config工具,先安裝pkg-config工具
deng@itcast:~/test01$ sudo apt install pkg-config編譯代碼
deng@itcast:~/test01$ g++ add_person.cc addressbook.pb.cc `pkg-config --cflags --libs protobuf` deng@itcast:~/test01$反單引號:反引號的作用就是將反引號內的linux命令執行
pkg-config 是通過庫提供的一個.pc文件獲得庫的各種必要信息的,包括版本信息、編譯和連接需要的參數等。
pkg-config –cflags protobuf:列出指定共享庫的預處理和編譯flags
運行可執行文件
deng@itcast:~/test01$ ./a.out pb.test pb.test: File not found. Creating a new file. Enter person ID number: 1 Enter name: deng Enter email address (blank for none): deng@itcast.cn Enter a phone number (or leave blank to finish): 110 Is this a mobile, home, or work phone? mobile Enter a phone number (or leave blank to finish): 120 Is this a mobile, home, or work phone? home Enter a phone number (or leave blank to finish): deng@itcast:~/test01$ ls add_person.cc addressbook.pb.h a.out pb.test addressbook.pb.cc addressbook.proto list_people.cc deng@itcast:~/test01$pb.test文件內容如下
deng@itcast:~/test01$ od -x pb.test 0000000 300a 040a 6564 676e 0110 0e1a 6564 676e 0000020 6940 6374 7361 2e74 6e63 0522 030a 3131 0000040 2230 0a07 3103 3032 0110 062a a508 bd9f 0000060 05ec 0000062 deng@itcast:~/test01$4.4 讀文件(反序列化)測試
deng@itcast:~/test01$ g++ list_people.cc addressbook.pb.cc `pkg-config --cflags --libs protobuf` deng@itcast:~/test01$ ./a.out pb.test Person ID: 1Name: dengE-mail address: deng@itcast.cnMobile phone #: 110Home phone #: 120Updated: 2019-09-28T12:18:45Z deng@itcast:~/test01$05. 參考
5.1 protobuf源碼下載: https://github.com/protocolbuffers/protobuf
5.2 protobuf官方參考文檔:https://developers.google.cn/protocol-buffers/
5.3 protobuf快速入門文檔:https://developers.google.cn/protocol-buffers/docs/tutorials
5.4 protobuf不同語言源碼下載:https://github.com/protocolbuffers/protobuf/releases
5.5 百度百科:https://baike.baidu.com/item/protocol%20buffer/1664400?fr=aladdin
5.6 簡書博客:https://www.jianshu.com/p/746bb9d81380
5.7 IBM技術文章:https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/
5.8 中文參考手冊(翻譯):https://www.cntofu.com/book/116/guide/index.md
總結
以上是生活随笔為你收集整理的【Protocol Buffer】Protocol Buffer入门教程(一):简介和安装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux】一步一步学Linux——a
- 下一篇: 【Protocol Buffer】Pro