protobuf下载地址:https://github.com/protocolbuffers/protobuf/releases
在运行./autogen.sh
或./configure
命令时出现"No such file or directory"
错误,是因为protobuf版本或构建方式的问题
如果没有configure
需要先运行./autogen.sh
生成
配置
./configure
// ./configure --prefix=/Users/xxx/protobuf(prefix 为安装目录,可自定义)
编译和安装
make
sudo make install
验证安装
protoc --version
libprotoc 3.21.12
安装位置
which protoc
/usr/local/bin/protoc
配置环境变量
vim ~/.zshrc
export PROTOBUF=/usr/local/bin/protoc
export PATH=$PROTOBUF/bin:$PATH
source ~/.zshrc
运行CMake配置
cmake .
报错,缺少 Abseil
库
CMake Error at third_party/utf8_range/CMakeLists.txt:31 (add_subdirectory):
The source directory
/xxx/protobuf-25.0/third_party/abseil-cpp
does not contain a CMakeLists.txt file.
https://github.com/protocolbuffers/protobuf/issues/12016
abseil-cpp下载
官方github:https://github.com/abseil/abseil-cpp/releases
单独安装:https://abseil.io/docs/cpp/quickstart-cmake#getting-the-abseil-code
在源码路径下开始编译
cmake . -DCMAKE_CXX_STANDARD=14
// -Dprotobuf_BUILD_TESTS=OFF //配置CMake以跳过测试构建
cmake --build .
安装
sudo make install
查看安装位置和全局版本
which protoc
protoc --version
不需要编译,直接使用,参考:https://grpc.io/docs/protoc-installation/
将这个包放入自定义文件夹,设置环境变量即可