douan3019 2011-07-26 20:13
浏览 28
已采纳

我可以做getest pass编译器标志吗?

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条回答 默认 最新

  • douju7765 2011-07-26 22:00
    关注

    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`.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟