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 -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -