Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created December 12, 2016 05:54
Show Gist options
  • Save jordanorelli/4091d8366f0651aad0fb8dff6ce82e21 to your computer and use it in GitHub Desktop.
Save jordanorelli/4091d8366f0651aad0fb8dff6ce82e21 to your computer and use it in GitHub Desktop.
minimum viable cgo with a header file
int from_c() {
return 12;
}
package main
import (
"fmt"
)
/*
#include "z.h"
*/
import "C"
func main() {
fmt.Printf("Hello. Value from C: %v\n", C.from_c())
}
int from_c();
@jordanorelli
Copy link
Author

put in a directory and build with go build. go build z.go or any variation of go run will not work, since specifying a filename tells the Go toolchain to only use those Go files and not other files, which thwarts cgo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment