Google Protocol Buffer
Google Protocol Buffer
在跨平臺通訊協議設計中,往往需要考慮後續跨語言的支援(如Java、Python、C/C++等),需求一種序列化、反序列化的資料結構。Google Protocol Buffer 提供了一種適用於RPC、持續資料儲存系統的混合語言資料標準,可用於通訊協議、資料儲存等領域的語言無關、平臺無關、可擴充套件的序列化結構資料格式。其支援有C++、Java、Python等等多種語言的API。
Protocol Buffer 一般具有如下優點:
- 通過資料結構的定義,能夠生成結構相關的介面程式碼
- 相容性好,支援對現有資料結構新增新成員
- 協議文字欄位自動壓縮,使用二進位制傳輸
protobuf
protobuf是google開發的序列化庫,其實現了Google Protocol Buffer定義的資料格式,並能根據檔案生成程式碼檔案,且只需要改動proto的定義就可以保持相容,非常的靈活方便。很多公司都使用它作為介面的資料結構。
但是官方沒給出c語言的支援,對於C語言的使用,我們需要使用另外的一個開源庫protobuf-c,其實現是依賴於protobuf開源庫,本文重點描述protobuf和protobuf-c的安裝及其使用。
一、protobuf的編譯安裝
參考:1、https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
protobuf開源在github上,其github地址是 https://github.com/protocolbuffers/protobuf
或者下載releases版本,本文章完成時,版本為protobuf-3.17.3。
以下步驟及操作都是在ubuntu20上進行編譯安裝。
在編譯之前,一些必要的依賴必須先安裝,像autoconf、automake、libtool,gcc/g++
sudo apt-get install autoconf automake libtool gcc g++
具體編譯步驟如下:
1、git clone https://github.com/protocolbuffers/protobuf.git 2、cd protobuf 3、./autogen.sh # 如果使用的不是git方式的原始碼,而是下載的release版本,已經包含gmock和configure指令碼,可以略過這一步 4、./configure --prefix=/usr/local/protobuf # 指定protobuf的安裝路徑 5、make #編譯,這一步很耗時間 6、sudo make install 7、sudo ldconfig # refresh shared library cache.
我們上面定義的安裝目錄預設情況下並不在系統路徑中,需要設定一下環境變數,只設置當前使用者的環境變數,我們只需要修改當前使用者的home目錄.bashrc檔案
開啟檔案.bashrc,在檔案的最後新增如下內容
# (動態庫搜尋路徑) 程式載入執行期間查詢動態連結庫時指定除了系統預設路徑之外的其他路徑
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib
# (靜態庫搜尋路徑) 程式編譯期間查詢動態連結庫時指定查詢共享庫的路徑
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/protobuf/lib
#指定protoc程式的路徑
export PATH=$PATH:/usr/local/protobuf/bin
檢視protobuf的版本資訊
protoc --version
二、protobuf-c的編譯安裝
protobuf-c開源地址在 https://github.com/protobuf-c/protobuf-c,
同樣的,我們也可以選擇下載releases版本,本文完成時的版本為protobuf-c 1.4.0。
以下步驟及操作都是在ubuntu20上進行編譯安裝。同樣的, 在編譯之前,也需要安裝相關的軟體依賴。
具體編譯步驟如下:
1、git clone https://github.com/protobuf-c/protobuf-c.git
2、cd protobuf-c
3、./autogen.sh # 如果使用的不是git方式的原始碼,而是下載的release版本,已經包含gmock和configure指令碼,可以略過這一步
4、./configure --prefix=/usr/local/protobuf-c # 指定protobuf-c的安裝路徑
5、make #編譯,這一步很耗時間
6、sudo make install
7、sudo ldconfig # refresh shared library cache.
注意,這個步驟中,第4步在執行configure時,可能會報如下的錯誤資訊
checking for protobuf... no
checking for protobuf... no
configure: error: Package requirements (protobuf >= 2.6.0) were not met:
No package 'protobuf' found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
Alternatively, you may set the environment variables protobuf_CFLAGS
and protobuf_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
提示我們找不到protobuf,但事實是我們已經正確安裝了,這個是因為Makefile中會用pkg-config命令檢測環境變數,但是沒有設定PKG_CONFIG_PATH,找不到protobuf.pc這個檔案,所以只要匯出這個即可
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig
此時,在重新執行第4步,得到如下結果:
protobuf-c 1.4.0
CC: gcc
CFLAGS: -g -O2
CXX: g++ -std=c++11
CXXFLAGS: -g -O2
LDFLAGS:
LIBS:
prefix: /usr/local/protobuf-c
sysconfdir: ${prefix}/etc
libdir: ${exec_prefix}/lib
includedir: ${prefix}/include
pkgconfigdir: ${libdir}/pkgconfig
bigendian: no
protobuf version: libprotoc 3.17.3
同樣的,我們自定義的路徑系統是沒法正確找到我們的protoc-c這個程式的,我們也需要將路徑寫入.bashrc這個檔案
開啟檔案.bashrc,在檔案的最後新增如下內容
# (動態庫搜尋路徑) 程式載入執行期間查詢動態連結庫時指定除了系統預設路徑之外的其他路徑
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf-c/lib
# (靜態庫搜尋路徑) 程式編譯期間查詢動態連結庫時指定查詢共享庫的路徑
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/protobuf-c/lib
#指定protoc-c程式的路徑
export PATH=$PATH:/usr/local/protobuf-c/bin
檢視protobuf的版本資訊
protoc-c --version
protobuf-c 1.4.0
libprotoc 3.17.3
本文就寫到這,下篇將介紹下,如何使用protobuf,不同語言之間的使用,有可能也聊下嵌入式環境下的如何使用等等。