我可以让gotest通过编译器标志吗?

huangapple go评论70阅读模式
英文:

Can I make gotest pass compiler flags?

问题

我刚刚组合了一个Go包,它将成为一个相当大的系统中的一部分,其中包含许多共享包。通过编写Makefile并使用-I标志调用编译器,我能够使其编译通过:

include $(GOROOT)/src/Make.inc

TARG=foobar
GOFILES=\
    foobar.go\

foobar:
    $(GC) -I$(CURDIR)/../intmath -I$(CURDIR)/../randnum foobar.go

include $(GOROOT)/src/Make.pkg

它可以正常编译,而且作为一个好孩子,我编写了一套全面的测试。然而,当我尝试使用gotest运行测试时,我得到了一个编译错误:

$ gotest
rm -f _test/foobar.a
8g  -o _gotest_.8 foobar.go  foobar_test.go
foobar.go:4: can't find import: intmath
make: *** [_gotest_.8] Error 1
gotest: "C:\msys\bin\sh.exe -c \"gomake\" \"testpackage\" \"GOTESTFILES=foobar_test.go\"" failed: exit status 2

所以,当我使用-I标志告诉它在哪里找到intmath和randnum包时,Go文件本身将编译,但是gotest似乎不使用Makefile。

回答peterSO的问题:
foobar.go的导入部分如下所示:

import (
    "intmath"
    "randnum"
    "container/vector"
)

只要我将-I标志传递给编译器,编译就可以正常工作。我尝试过使用相对路径,像这样:

import (
    "../intmath"
    "../randnum"
    "container/vector"
)

但是似乎不起作用。

编辑:回答进一步的peterSO问题:

GOROOT设置为C:\Go,这是我安装所有Go相关内容(除了我的源代码)的目录。我希望相对路径是相对于源文件所在的目录。

我的源代码树如下所示:

server/
    foobar/
    randnum/
    intmath/

因此,虽然我可以接受不同的、更符合Go习惯的目录结构,但我的直觉是将它们排列为同级。

有没有办法让gotest编译foobar.go并使用所需的标志?

英文:

I have just put together a Go package that is going to be a part in a fairly large system with a lot of shared packages. I was able to get it to compile by writing its Makefile such that the compiler is called with -I flags:

include $(GOROOT)/src/Make.inc

TARG=foobar
GOFILES=\
    foobar.go\

foobar:
    $(GC) -I$(CURDIR)/../intmath -I$(CURDIR)/../randnum foobar.go

include $(GOROOT)/src/Make.pkg

It compiles just fine, and being a good boy, I wrote a comprehensive set of tests. However, when I try to run the tests with gotest, I get a compile error:

$ gotest
rm -f _test/foobar.a
8g  -o _gotest_.8 foobar.go  foobar_test.go
foobar.go:4: can't find import: intmath
make: *** [_gotest_.8] Error 1
gotest: "C:\\msys\\bin\\sh.exe -c \"gomake\" \"testpackage\" \"GOTESTFILES=foobar_test.go\"" failed: exit status 2

So, the Go file itself will compile when I use the -I flags to tell it where to find the intmath and randnum packages, but gotest doesn't seem to use the Makefile.

Answering peterSO's question:
foobar.go's import section looks like this:

import (
    "intmath"
    "randnum"
    "container/vector"
)

And the compile works fine as long as I have the -I flags going to the compiler. I have tried to use relative paths, like this:

import (
    "../intmath"
    "../randnum"
    "container/vector"
)

but that just doesn't seem to work.

EDIT: answering further peterSO questions:

GOROOT is set to C:\Go the directory where I have all of the Go stuff -- aside from my source code -- installed. I was expecting the relative path to be relative to the directory in which the source file lives.

My source tree looks like this:

server/
    foobar/
    randnum/
    intmath/

So, while I am open to a different, more Go-idiomatic directory structure, my instinct is to arrange them as peers.

Is there some way that I can nudge gotest into compiling foobar.go with the needed flags?

答案1

得分: 1

创建Windows源代码目录结构:

C:\server
C:\server\foobar
C:\server\intnum

对于intnum.go:

package intnum

func IntNum() int {
	return 42
}

Makefile:

include $(GOROOT)/src/Make.inc
TARG=server/intnum
GOFILES=\
	intnum.go\
include $(GOROOT)/src/Make.pkg

运行:

$ cd c/server/intnum
$ make install

对于foobar.go:

package foobar

import (
	"math"
	"server/intnum"
)

func FooBar() float64 {
	return float64(intnum.IntNum()) * math.Pi
}

Makefile:

include $(GOROOT)/src/Make.inc
TARG=server/foobar
GOFILES=\
	foobar.go\
include $(GOROOT)/src/Make.pkg

运行:

$ cd /c/server/foobar
$ make install

安装完成后,intnum.afoobar.a包文件将位于$GOROOT\pkg\windows_386\serverC:\Go\pkg\windows_386\server)目录中。

英文:

Create the Windows source code directory structure:

C:\server
C:\server\foobar
C:\server\intnum

For intnum.go:

package intnum

func IntNum() int {
	return 42
}

Makefile:

include $(GOROOT)/src/Make.inc
TARG=server/intnum
GOFILES=\
	intnum.go\
include $(GOROOT)/src/Make.pkg

Run:

$ cd c/server/intnum
$ make install

For foobar.go:

package foobar

import (
	"math"
	"server/intnum"
)

func FooBar() float64 {
	return float64(intnum.IntNum()) * math.Pi
}

Makefile:

include $(GOROOT)/src/Make.inc
TARG=server/foobar
GOFILES=\
	foobar.go\
include $(GOROOT)/src/Make.pkg

Run:

$ cd /c/server/foobar
$ make install

After the install, the intnum.a and foobar.a package files will be in the $GOROOT\pkg\windows_386\server (C:\Go\pkg\windows_386\server) directory`.

huangapple
  • 本文由 发表于 2011年7月27日 04:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/6836006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定