tags:

views:

512

answers:

2

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?

+4  A: 

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

ppierre
+4  A: 

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.

Scott Wales