From a basic test program. . .
package main
/*
#i开发者_开发问答nclude <stdio.h>
static void test() {
printf("hello world");
}
*/
import "C"
func main() {
C.test();
}
I do "cgo hello_cgo.go" and get:
_cgo_.o
_cgo_defun.c
_cgo_gotypes.go
hello_cgo.cgo1.go
hello_cgo.cgo2.c
How do I go about compiling from here to an exe?
Try using the go makefiles. Create a makefile like
# Makefile
CGOFILES=test.go
TARG=test
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.pkg
Running make will then produce the file _obj/test.a
, which you'll have to link with 6l
or similar.
Update for go1:
$ cat foo.go
package main
// #include <stdio.h>
// static void test() { printf("Hello, world\n"); }
import "C"
func main() {
C.test()
}
$ go build foo.go
$ ./foo
Hello, world
精彩评论