【问题标题】:protoc in makefile to build protobufmakefile 中的 protoc 来构建 protobuf
【发布时间】:2018-03-03 14:46:05
【问题描述】:

我已经安装了一个协议缓冲区,就像https://github.com/google/protobuf/blob/master/src/README.md上的教程一样

之后我想构建 protobuf 并使用以下命令安装 go 库:

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go
make # generate app and protobuf

我的根目录上有文件Makefile,如下所示:

get:
    echo "Build Proto"
    protoc -I/usr/local/include -I. -I$GOPATH/src -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis proto/item.proto --go_out=plugins=grpc:.
    protoc -I/usr/local/include -I. -I$GOPATH/src -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis proto/item.proto --grpc-gateway_out=logtostderr=true:.
    protoc -I/usr/local/include -I. -I$GOPATH/src -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis proto/item.proto --swagger_out=logtostderr=true:.
    echo "Build APP"
    CGO_ENABLED=0 GOOS=linux go build -o ./server/storeitemservice ./server/cmd/server/main.go

但我想使用命令make 在我的应用程序根目录中生成应用程序和protobuf 来构建我的应用程序,结果如下:

echo "Build Proto"
Build Proto
protoc -I/usr/local/include -I. -IOPATH/src -IOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis proto/item.proto --go_out=plugins=grpc:.
OPATH/src: warning: directory does not exist.
OPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis: warning: directory does not exist.
google/api/annotations.proto: File not found.
proto/item.proto: Import "google/api/annotations.proto" was not found or had errors.
Makefile:2: recipe for target 'get' failed
make: *** [get] Error 1

在看到这样的问题后,我检查了它的每个目录,结果发现全部都有。

【问题讨论】:

    标签: go protocol-buffers grpc


    【解决方案1】:

    你需要像这样包装你的 $GOPATH

    protoc -I/usr/local/include -I. -I$(GOPATH)/src \
           -I$(GOPATH)/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
           proto/item.proto --go_out=plugins=grpc:.
    

    添加\ 是为了便于在此处阅读。关键是$(GOPATH) vs $GOPATH

    这是一个演示:

    get:
        echo $(GOPATH)
        echo $GOPATH
    

    和输出

    echo /Users/sberry/Development/golang
    /Users/sberry/Development/golang
    echo OPATH
    OPATH
    

    在了解 Makefile 语法的编辑器中,您可以看到差异

    【讨论】:

    • 您的回答对预期很有帮助,谢谢@sberry
    猜你喜欢
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    相关资源
    最近更新 更多