不管是因為什麼原因需要,整合第三方套件總是要面對一些 breaking change,甚至在大的工程團隊中,每個專案的版本不一致導致連環狀況發生。要裝 protobuf 特定版本其實不難,將以 3.5.1 版本為例:

安裝該版本原始碼

首先可以在 protobuf 的 github 專案中找到不同版本的原始碼載點,找到自己的需求後下載: https://github.com/protocolbuffers/protobuf/releases

解壓縮後,在編譯 protobuf 時可能需要以下幾個套件,可以用 homebrew 做安裝:

$ brew install autoconf
$ brew install automake
$ brew install libtool

接下來就是對這份原始碼作 compile 與安裝(記得進到解壓縮後資料夾內):

$ ./autogen.sh
$ ./configure
$ make
$ make install

檢查是否成功:

protoc —version

如何擁有多個版本

關鍵在於如何讓 `protoc` 這指令關連到想要的該版本,所以我們可以在 bash_profile 或是 zshrc 內加上環境參數

export PROTOBUF=/Users/hothero/protobuf35 # 記得是放你原始碼解壓縮後資料夾位置
export PATH=$PROTOBUF/bin:$PATH

接著執行套用新設定即可生效

 source ~/.zshrc