Kawili-wili
Kawili-wili

Reputation: 439

How to get from cgo to exe

From a basic test program. . .

package main

/*
#include <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?

Upvotes: 3

Views: 1239

Answers (2)

serbaut
serbaut

Reputation: 5960

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

Upvotes: 2

Scott Wales
Scott Wales

Reputation: 11696

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.

Upvotes: 6

Related Questions