Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created May 12, 2020 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/821abde4ec13bf54fac5b4ac30d2c96a to your computer and use it in GitHub Desktop.
Save peterhellberg/821abde4ec13bf54fac5b4ac30d2c96a to your computer and use it in GitHub Desktop.
Example of c-shared libhello.so
#include <stdio.h>
#include "libhello.h"
int main() {
printf("Hello World from C!\n");
printf("%s\n", helloWorld());
return 0;
}
package main
import "C"
//export helloWorld
func helloWorld() *C.char {
return C.CString("Hello World from Go!")
}
func main() {}
.PHONY: all build compile clean
all: build compile
build:
go build -buildmode=c-shared -o libhello.so libhello.go
compile:
cc -o hello hello.c libhello.so
clean:
rm hello libhello.h libhello.so
@peterhellberg
Copy link
Author

$ ./hello
Hello World from C!
Hello World from Go!

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