环境:ubuntu18.04
1.安装protobuf2.6.1
克隆代码
git clone https://github.com/google/protobuf.git
cd protobuf
git checkout -b 2.6.1 origin/2.6.1-artifacts
git submodule update --init --recursive
安装
./autogen.sh
./configure
make
make test
sudo make install
sudo ldconfig
执行./autogen.sh时出了个错误,报错如下:
Google Test not present, Fetching gtest-1.5.0 from web ...
参考https://blog.csdn.net/changer_sun/article/details/79264326
原因就是autogen.sh脚本中的gtest包地址已经变了,所以手动从github获取gtest包,或者修改autogen脚本都行
https://github.com/google/googletest/tree/release-1.5.0
获取包后解压放到protobuf目录下,重命名为gtest
查看proto版本
protoc --version
2.安装protoc-gen-go
安装proto3版本对应的protoc-gen-go很简单
go get -d -u github.com/golang/protobuf/protoc-gen-go
然而我安装的是protobuf2.6.1,所以就要安装对应的protoc-gen-go版本
参考链接https://blog.csdn.net/u010918487/article/details/89003747
执行以下命令
GIT_TAG="v1.2.0" # change as needed
go get -d -u github.com/golang/protobuf/protoc-gen-go
git -C "$(go env GOPATH)"/src/github.com/golang/protobuf checkout $GIT_TAG
go install github.com/golang/protobuf/protoc-gen-go
生成pb文件
protoc --go_out=$GOPATH/test -I=$GOPATH/test $GOPATH/test/test.proto
注意是否配置了GOPATH
vim ~/.zshrc
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$GOBIN:$PATH
不配置GOPATH会导致protoc-gen-go命令找不到
-I[--proto_path]参数proto文件所在的目录,要和后面的proto文件路径一致,不然也会报错