开发者

How to get from cgo to exe

开发者 https://www.devze.com 2022-12-29 17:25 出处:网络
From a basic test program. . . package main /* #i开发者_开发问答nclude <stdio.h> static void test() {

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号