Mac下protobuf生成檔案報錯問題解決辦法,windows下就不會這么麻煩了,如果linux下出現類似報錯資訊按照下面的解決邏輯依然適用,
1、由--go_out引發的報錯
1.報錯資訊:
user@C02FP58GML7H pbfile % protoc --go_out=./ ./user.proto
protoc-gen-go: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go_out: protoc-gen-go: Plugin failed with status code 1.
2.解決辦法:
- 找到protoc-gen-go 檔案,復制檔案到
/usr/local/bin/目錄下,該檔案通過go get安裝時會默認安裝在$GOPATH/go/bin目錄下,博主本人的安裝路徑/Users/user/go/bin
執行復制命令:cp protoc-gen-go /usr/local/bin/ - 修改環境變數配置:vim ~/.bash_profile
添加:export GOPATH=$HOME/go PATH=$PATH:$GOPATH/bin
重繪環境變數:source ~/.bash_profile
3.結果示例:
user@C02FP58GML7H bin % vim ~/.bash_profile
user@C02FP58GML7H bin % source ~/.bash_profile
/Users/user/.bash_profile:source:3: no such file or directory: /usr/local/bin/virtualenvwrapper.sh
2、由--go-grpc_out和protoc-gen-go-grpc引發的報錯
1.按照官方命令生成檔案:
# product.proto是博主本人的檔案,執行的時候需要修
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative product.proto
2.報錯資訊如下:
user@C02FP58GML7H pbfile % protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative product.proto
protoc-gen-go-grpc: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.
3.重新安裝一下:
sudo go get -u google.golang.org/protobuf/cmd/protoc-gen-go
sudo go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc
4.將protoc-gen-go和protoc-gen-go-grpc復制到/usr/local/bin/目錄下
使用go env查看GOPATH的路徑,默認安裝在這個檔案夾下的bin檔案夾中
- 切換目錄:cd /Users/user/go/bin(博主的所在位置)
- 復制:cp protoc-gen-go /usr/local/bin/
- 復制:cp protoc-gen-go-grpc /usr/local/bin/
5.檢查/添加環境變數
使用命令cat ~/.bash_profile查看環境變數中是否存在:export PATH=/usr/local/go/bin
存在 重繪一下環境變數配置:source ~/.bash_profile
不存在 使用vim添加:export PATH=/usr/local/go/bin
重繪一下環境變數配置:source ~/.bash_profile
6.執行protoc命令生成檔案即可
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/509783.html
標籤:其他
