多包的Go语言Makefile示例

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

multi package makefile example for go

问题

我正在尝试设置一个类似的多包Go项目:

./main.go

./subpackage1/sub1_1.go

./subpackage1/sub1_2.go

./subpackage2/sub2_1.go

./subpackage2/sub2_2.go

其中main.go导入了subpackage1和subpackage2。而subpackage2又导入了subpackage1。

我一直在寻找Go Makefile的示例,但是我找不到支持这种设置的任何内容。有什么建议吗?

英文:

I'm trying to setup a multi package go project something like

./main.go

./subpackage1/sub1_1.go

./subpackage1/sub1_2.go

./subpackage2/sub2_1.go

./subpackage2/sub2_2.go

where main.go imports both subpackage1 and subpackage2. And subpackage2 imports subpackage1.

Ive been looking around for go makefile examples but I can't find anything that supports this kind of set-up. Any idea?

答案1

得分: 17

安装godag然后运行:

gd -o myapp

它会自动构建一个有向无环图(DAG),包含你的src/目录中所有依赖项,然后按正确的顺序编译和链接每个包。

比手动维护Makefile要简单得多,特别是因为最近的Go版本中$(GOROOT)/src/Make.*已经发生了变化(不再有Make.$(GOARCH))。还有一些有用的命令:

gd clean用于删除对象文件。

gd -test运行自动化测试(参见testing包)。

gd -dot=myapp.dot生成一个包导入的图形,你可以使用GraphViz进行可视化。

英文:

Install godag then run:

gd -o myapp

It will automatically build a Directed Acyclic Graph (DAG) of all the dependencies in your src/ directory, then compile and link each package in the proper order.

Much easier than manually maintaining a Makefile, especially since $(GOROOT)/src/Make.* has changed in recent versions of Go (there is no longer a Make.$(GOARCH)). Also useful:

gd clean removes object files.

gd -test runs your automated tests (see testing package).

gd -dot=myapp.dot generates a graph of your package imports you can visualize using GraphViz.

答案2

得分: 7

这样应该可以工作

# Makefile
include $(GOROOT)/src/Make.$(GOARCH)
all:main
main:main.$O
    $(LD) -Lsubpackage1/_obj -Lsubpackage2/_obj -o $@ $^
%.$O:%.go  subpackage1 subpackage2
    $(GC) -Isubpackage1/_obj -Isubpackage2/_obj -o $@ $^
subpackage1:
    $(MAKE) -C subpackage1
subpackage2:
    $(MAKE) -C subpackage2
.PHONY:subpackage1 subpackage2

# subpackage1/Makefile
TARG=subpackage1
GOFILES=sub1_1.go sub1_2.go
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.pkg

# subpackage2/Makefile
TARG=subpackage2
GOFILES=sub2_1.go sub2_2.go
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.pkg
GC+=-I../subpackage1/_obj
LD+=-L../subpackage1/_obj
sub2_1.$O sub2_2.$O:subpackage1
subpackage1:
    $(MAKE) -C ../subpackage1
.PHONY:subpackage1

如果您没有安装所需的子包,您需要显式设置包含路径。提供的Make.pkg文件主要用于构建包,这就是为什么它只包含在子包的makefile中的原因。

英文:

Something like this should work

# Makefile
include $(GOROOT)/src/Make.$(GOARCH)
all:main
main:main.$O
    $(LD) -Lsubpackage1/_obj -Lsubpackage2/_obj -o $@ $^
%.$O:%.go  subpackage1 subpackage2
    $(GC) -Isubpackage1/_obj -Isubpackage2/_obj -o $@ $^
subpackage1:
    $(MAKE) -C subpackage1
subpackage2:
    $(MAKE) -C subpackage2
.PHONY:subpackage1 subpackage2

# subpackage1/Makefile
TARG=subpackage1
GOFILES=sub1_1.go sub1_2.go
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.pkg

# subpackage2/Makefile
TARG=subpackage2
GOFILES=sub2_1.go sub2_2.go
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.pkg
GC+=-I../subpackage1/_obj
LD+=-L../subpackage1/_obj
sub2_1.$O sub2_2.$O:subpackage1
subpackage1:
    $(MAKE) -C ../subpackage1
.PHONY:subpackage1

If you don't install the subpackages you need to explicitly set the include path. The supplied makefile Make.pkg is mainly to build packages, which is why it's only included in the subpackage makefile.

答案3

得分: 5

hello world with a Makefile and a test(谷歌群组:golang-nuts)

英文:

hello world with a Makefile and a test (Googles Groupes : golang-nuts)

答案4

得分: 4

请查看 https://github.com/banthar/Go-SDL,这是一个使用Makefiles进行维护的多包Go项目。

我注意到有些答案使用了过时的Make.$(GOARCH)包含方式。希望上面的链接比在这里跟踪Google不断变化的API更加稳定。

英文:

Check out https://github.com/banthar/Go-SDL which is an actively maintained multi-package go project using Makefiles.

I notice some of these the answers use the obsolete Make.$(GOARCH) include. So hopefully the above link will be stabler than trying to stay on top of Google's changing API in an answer here.

huangapple
  • 本文由 发表于 2009年11月20日 05:33:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/1766720.html
匿名

发表评论

匿名网友

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

确定