import - How to use external .c files with CGO? -


to write c code in comment above import "c" straightforward:

// foo.go package main  /* int fortytwo() {     return 42; } */ import "c" import "fmt"  func main() {     fmt.printf("forty-two == %d\n", c.fortytwo())     fmt.printf("forty-three == %d\n", c.fortythree()) } 

and works fine:

$ go install $ foo forty-two == 42 

however, c code in it's own .c file:

// foo.c int fortythree() {   return 43; } 

...referenced go:

// foo.go func main() {     fmt.printf("forty-two == %d\n", c.fortytwo())     fmt.printf("forty-three == %d\n", c.fortythree()) } 

...does not work:

$ go install # foo not determine kind of name c.fortythree 

the c header file foo.h missing:

// foo.h int fortythree(); 

reference header file go this:

// foo.go package main  /* #include "foo.h"  int fortytwo() {     return 42; } */ import "c" import "fmt"  func main() {     fmt.printf("forty-two == %d\n", c.fortytwo())     fmt.printf("forty-three == %d\n", c.fortythree()) } 

behold, power of foo.h:

$ go install $ foo forty-two == 42 forty-three == 43 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -