2010-05-07 28 views
3

從基本的測試程序。 。 。如何從cgo到exe

package main 

/* 
#include <stdio.h> 
static void test() { 
     printf("hello world"); 
} 
*/ 
import "C" 

func main() { 
     C.test(); 
} 

我願意 「CGO hello_cgo.go」 並獲得:

_cgo_.o 
_cgo_defun.c 
_cgo_gotypes.go 
hello_cgo.cgo1.go 
hello_cgo.cgo2.c 

如何去編譯從這裏到一個exe?

回答

6

嘗試使用go makefiles。創建像

# Makefile 
CGOFILES=test.go 
TARG=test 

include $(GOROOT)/src/Make.$(GOARCH) 
include $(GOROOT)/src/Make.pkg 

運行make makefile文件,然後將生成的文件_obj/test.a,你就會有6l或類似的鏈接。

+0

謝謝!這工作。欣賞快速反應。 – 2010-05-07 02:26:13

2

更新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