#include "life.h"
#include "_cgo_export.h"
+const int MYCONST = 0;
+
// Do the actual manipulation of the life board in C. This could be
// done easily in Go, we are just using C for demonstration
// purposes.
//export GoStart
// Double return value is just for testing.
func GoStart(i, xdim, ydim, xstart, xend, ystart, yend C.int, a *C.int, n *C.int) (int, int) {
- c := make(chan bool)
+ c := make(chan bool, int(C.MYCONST))
go func() {
C.DoStep(xdim, ydim, xstart, xend, ystart, yend, a, n)
c <- true
extern void Step(int, int, int *, int *);
extern void DoStep(int, int, int, int, int, int, int *, int *);
+extern const int MYCONST;
var Stdout = (*File)(C.stdout)
var Stderr = (*File)(C.stderr)
+// Test reference to library symbol.
+// Stdout and stderr are too special to be a reliable test.
+var myerr = C.sys_errlist
+
func (f *File) WriteString(s string) {
p := C.CString(s)
C.fputs(p, (*C.FILE)(f))
CGOPKGPATH=$(dir) cgo -- $(CGO_CFLAGS) $(CGOFILES)
endif
-# Ugly but necessary
-_cgo_gotypes.go _cgo_export.c _cgo_export.h: _cgo_defun.c
+# Ugly but necessary - cgo writes these files too.
+_cgo_gotypes.go _cgo_export.c _cgo_export.h _cgo_main.c: _cgo_defun.c
@true
%.cgo1.go %.cgo2.c: _cgo_defun.c
# and libraries are involved, instead of duplicating gcc's logic ourselves.
# After main we have to define all the symbols that will be provided
# by Go code. That's crosscall2 and any exported symbols.
-_cgo_main.c: _cgo_defun.c
- echo 'int main() { return 0; }' >$@
- echo 'int crosscall2;' >>$@
- awk -F'(' '/^_cgoexp_/ {print "int " $$1 ";"}' _cgo_defun.c >>$@
_cgo_main.o: _cgo_main.c
$(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ -c $(CGO_CFLAGS) _cgo_main.c
fgo2 := creat("_cgo_gotypes.go")
fc := creat("_cgo_defun.c")
+ fm := creat("_cgo_main.c")
+
+ // Write C main file for using gcc to resolve imports.
+ fmt.Fprintf(fm, "int main() { return 0; }\n")
+ fmt.Fprintf(fm, "int crosscall2;\n\n")
// Write second Go output: definitions of _C_xxx.
// In a separate file so that the import of "unsafe" does not
}
cVars = append(cVars, n.C)
+ fmt.Fprintf(fm, "extern char %s[];\n", n.C)
+ fmt.Fprintf(fm, "void *_cgohack_%s = %s;\n\n", n.C, n.C)
+
fmt.Fprintf(fc, "extern byte *%s;\n", n.C)
fmt.Fprintf(fc, "void *·%s = &%s;\n", n.Mangle, n.C)
fmt.Fprintf(fc, "\n")
}
}
- p.writeExports(fgo2, fc)
+ p.writeExports(fgo2, fc, fm)
fgo2.Close()
fc.Close()
// Write out the various stubs we need to support functions exported
// from Go so that they are callable from C.
-func (p *Package) writeExports(fgo2, fc *os.File) {
+func (p *Package) writeExports(fgo2, fc, fm *os.File) {
fgcc := creat("_cgo_export.c")
fgcch := creat("_cgo_export.h")
fmt.Fprintf(fc, "\truntime·cgocallback(·%s, a, n);\n", goname)
fmt.Fprintf(fc, "}\n")
+ fmt.Fprintf(fm, "int _cgoexp%s_%s;\n", cPrefix, exp.ExpName)
+
// Calling a function with a receiver from C requires
// a Go wrapper function.
if fn.Recv != nil {