Created
February 12, 2022 09:43
-
-
Save fasionchan/f58b6ae9efc89008cb129debe093a9f0 to your computer and use it in GitHub Desktop.
Call C function in Go | Go语言调用C函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "callee.h" | |
void SayHello() { | |
printf("Hello, world!\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void SayHello(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#cgo CFLAGS: -I. | |
#cgo LDFLAGS: -L. -lcallee | |
#include "callee.h" | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
) | |
func main() { | |
C.SayHello() | |
fmt.Println("Success!") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.DEFAULT_GOAL := run | |
callee.o: callee.c | |
gcc -fPIC -c callee.c | |
libcallee.so: callee.o | |
gcc -shared -o $@ $^ | |
caller: caller.go libcallee.so | |
go build caller.go | |
clean: | |
rm -f caller libcallee.so callee.o | |
run: caller | |
./caller |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment