【工具使用】protobuf 安装


应对不同开发需要,本机可能需要安装多个版本的protobuf,整理了一下不同版本的二进制安装方式,及不同版本切换方法
官方安装包下载链接 https://github.com/protocolbuffers/protobuf/releases
当然mac环境也可能通过brew安装,这里不做介绍

v2.6.1 安装

最高版本 https://github.com/protocolbuffers/protobuf/releases/tag/v2.6.1

# 一、 2.x 安装
tar zxf protobuf-2.6.1.tar.gz

$ cd protobuf-2.6.1
# ./configure # 默认安装到/usr/local
# 为了便于2和3版本切换,安装到指定目录
$ ./configure --prefix=/Users/momo/devTools/protobuf-2.6.1
$ make check
$ make
$ sudo make install # 该步后在protobuf-2.6.1目录下会有bin目录,protoc脚本在此
$ protoc --version

# 二、 2.x 卸载,比如在默认路径下
$ which protoc
/usr/local/bin/protoc
$ sudo rm /usr/local/bin/protoc

v2.5.0 安装

下载地址:https://github.com/protocolbuffers/protobuf/releases/tag/v2.5.0
旧版本的protobuf 2.5.0不支持mac m1,需要修改源码
mac m1 2.5.0 安装参考文档
mac m1环境需要修改源码:

// 修改源码 src/google/protobuf/stubs/platform_macros.h
// 找到
#else
#error Host architecture was not detected as supported by protobuf
// 在其上方添加
#elif defined(__arm64__)
#define GOOGLE_PROTOBUF_ARCH_ARM 1
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
// 最终内容如下:
#elif defined(__arm64__)
#define GOOGLE_PROTOBUF_ARCH_ARM 1
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
#else
#error Host architecture was not detected as supported by protobuf

安装:

./configure --prefix=/Users/momo/devTools/protobuf-2.5.0
make check
make
sudo make install # 该步后在protobuf-2.5.0目录下会有bin目录,protoc脚本在此

v3.19.4 安装

# 3.x 安装,可直接下载编译好的
unzip protoc-3.19.4-osx-x86_64.zip

cat readme.txt
Protocol Buffers - Google's data interchange format
Copyright 2008 Google Inc.
https://developers.google.com/protocol-buffers/

This package contains a precompiled binary version of the protocol buffer
compiler (protoc). This binary is intended for users who want to use Protocol
Buffers in languages other than C++ but do not want to compile protoc
themselves. To install, simply place this binary somewhere in your PATH.

If you intend to use the included well known types then don't forget to
copy the contents of the 'include' directory somewhere as well, for example
into '/usr/local/include/'.

Please refer to our official github site for more installation instructions:
  https://github.com/protocolbuffers/protobuf

# 需要将/Users/momo/devTools/protoc-3.19.4/bin/protoc拷贝至/usr/local/bin/
sudo cp /Users/momo/devTools/protoc-3.19.4/bin/protoc /usr/local/bin/
sudo chmod a+x /usr/local/bin/protoc

版本切换

# 切换至2.6.1
sudo cp /Users/momo/devTools/protobuf-2.6.1/bin/protoc /usr/local/bin/
sudo chmod a+x /usr/local/bin/protoc

# 切换至2.5.0
sudo cp /Users/momo/devTools/protobuf-2.5.0/bin/protoc /usr/local/bin/
sudo chmod a+x /usr/local/bin/protoc

# 切换至3.x
sudo cp /Users/momo/devTools/protoc-3.19.4/bin/protoc /usr/local/bin/
sudo chmod a+x /usr/local/bin/protoc