README: How support C and Python

This commit is contained in:
xushiwei
2024-07-14 10:56:19 +08:00
parent 7390afc5e1
commit 5ba01674fb
2 changed files with 49 additions and 0 deletions

View File

@@ -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!