From 5ba01674fb5ea57a03d0957c6320b7ed935845db Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 14 Jul 2024 10:56:19 +0800 Subject: [PATCH] README: How support C and Python --- README.md | 39 ++++++++++++++++++++++++++++++++++++++ _demo/linkname/linkname.go | 10 ++++++++++ 2 files changed, 49 insertions(+) create mode 100644 _demo/linkname/linkname.go diff --git a/README.md b/README.md index 995a11ef..c6c1a4f2 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,45 @@ LLGo := Go + C + Python LLGo is compatible with C and Python through the language's **Application Binary Interface (ABI)**, while LLGo is compatible with Go through its **syntax (source code)**. +## How support C and Python + +LLGo use `go:linkname` to link an extern symbol througth its **Application Binary Interface (ABI)**: + +```go +import _ "unsafe" // for go:linkname + +//go:linkname Sqrt C.sqrt +func Sqrt(x float64) float64 +``` + +You can directly integrate it into [your own code](_demo/linkname/linkname.go): + +```go +package main + +import _ "unsafe" // for go:linkname + +//go:linkname Sqrt C.sqrt +func Sqrt(x float64) float64 + +func main() { + println("sqrt(2) =", Sqrt(2)) +} +``` + +Or put it into a package (see [c/math](c/math/math.go)): + +```go +package main + +import "github.com/goplus/llgo/c/math" + +func main() { + println("sqrt(2) =", math.Sqrt(2)) +} +``` + + ## C/C++ standard libary support You can import a C/C++ standard library in LLGo! diff --git a/_demo/linkname/linkname.go b/_demo/linkname/linkname.go new file mode 100644 index 00000000..b9c1686e --- /dev/null +++ b/_demo/linkname/linkname.go @@ -0,0 +1,10 @@ +package main + +import _ "unsafe" // for go:linkname + +//go:linkname Sqrt C.sqrt +func Sqrt(x float64) float64 + +func main() { + println("sqrt(2) =", Sqrt(2)) +}