Go makefile中的protoc用于构建protobuf

Go makefile中的protoc用于构建protobuf,go,protocol-buffers,grpc,Go,Protocol Buffers,Grpc,我已经安装了一个协议缓冲区,就像上的教程一样 之后,我想构建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 #

我已经安装了一个协议缓冲区,就像上的教程一样

之后,我想构建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
我的根目录上有file
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
在我的应用程序根目录中生成app和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

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

您需要像这样包装$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