如果没有设置GOPATH,则不可能对go程序进行编译。但是许多go项目都是使用Makefile构建的,因为go也没有提取git修订版、设置版本等功能,因此应该可以从Makefile中自动检测GOPATH。
假设我为go get -d手动设置了一次GOPATH
go get -d github.com/zyedidia/micro/cmd/micro现在,如果我打开另一个会话,将cd转换为github.com/zyedidia/micro/cmd/micro并执行make build,则构建失败:
...
cmd/micro/micro.go:20:2: cannot find package "layeh.com/gopher-luar" in any of:
/usr/lib/go-1.7/src/layeh.com/gopher-luar (from $GOROOT)
($GOPATH not set)
Makefile:15: recipe for target 'build' failed
make: *** [build] Error 1因此,如果没有设置GOPATH,我如何从Makefile中设置它,并确保此时存在go环境?
这不管用:
GOPATH ?= ../../../..UPDATE:下面的代码工作,但它没有检测到父目录包含src、bin和pkg dirs。
export GOPATH ?= $(abspath $(dir ../../../../..))需要export将make变量转换为环境变量,?=集make变量仅为未设置的变量,abspath和dir在此描述:
发布于 2017-07-23 07:09:33
这是解决办法。
# detect GOPATH if not set
ifndef $(GOPATH)
$(info GOPATH is not set, autodetecting..)
TESTPATH := $(dir $(abspath ../../..))
DIRS := bin pkg src
# create a ; separated line of tests and pass it to shell
MISSING_DIRS := $(shell $(foreach entry,$(DIRS),test -d "$(TESTPATH)$(entry)" || echo "$(entry)";))
ifeq ($(MISSING_DIRS),)
$(info Found GOPATH: $(TESTPATH))
export GOPATH := $(TESTPATH)
else
$(info ..missing dirs "$(MISSING_DIRS)" in "$(TESTDIR)")
$(info GOPATH autodetection failed)
endif
endif我学到的是:
echo在这个块中不工作,需要使用$(info)发布于 2017-12-19 02:23:19
我遇到了同样的问题,以下是我的解决方案:
ifndef $(GOPATH)
GOPATH=$(shell go env GOPATH)
export GOPATH
endifhttps://stackoverflow.com/questions/45261101
复制相似问题