purego
[Go Reference]
A library for calling C functions from Go without Cgo.
Fork notice: this repository is a maintained fork of github.com/ebitengine/purego with additional ABI, callback, and generator-facing fixes needed by the PureGoTK stack.
This is beta software so expect bugs and potentially API breaking changes
but each release will be tagged to avoid breaking people's code.
Bug reports are encouraged.
Motivation
The Ebitengine game engine was ported to use only Go on Windows. This enabled
cross-compiling to Windows from any other operating system simply by setting GOOS=windows. The purego project was
born to bring that same vision to the other platforms supported by Ebitengine.
Benefits
- Simple Cross-Compilation: No C means you can build for other platforms easily without a C compiler.
- Faster Compilation: Efficiently cache your entirely Go builds.
- Smaller Binaries: Using Cgo generates a C wrapper function for each C function called. Purego doesn't!
- Dynamic Linking: Load symbols at runtime and use it as a plugin system.
- Foreign Function Interface: Call into other languages that are compiled into shared objects.
- Cgo Fallback: Works even with CGO_ENABLED=1 so incremental porting is possible.
This also means unsupported GOARCHs (freebsd/riscv64, linux/mips, etc.) will still work
except for float arguments and return values.
Tier 1
Tier 1 platforms are the primary targets officially supported by PureGo. When a new version of PureGo is released, any critical bugs found on Tier 1 platforms are treated as release blockers. The release will be postponed until such issues are resolved.
- Android: amd641, arm641
- iOS: amd641, arm641
- Linux: amd64, arm64
- macOS: amd64, arm64
- Windows: amd64, arm64
Tier 2
Tier 2 platforms are supported by PureGo on a best-effort basis. Critical bugs on Tier 2 platforms do not block new PureGo releases. However, fixes contributed by external contributors are very welcome and encouraged.
- Android: 3861, arm1
- FreeBSD: amd642, arm642
- Linux: 386, arm, loong64, ppc64le, riscv64, s390x1
- NetBSD: amd642, arm642
- Windows: 3863, arm3,4
Support Notes
- These architectures require CGO_ENABLED=1 to compile
- These architectures require the special flag
-gcflags="github.com/bnema/purego/internal/fakecgo=-std" to compile with CGO_ENABLED=0
- These architectures only support
SyscallN and NewCallback
- These architectures are no longer supported as of Go 1.26
Example
The example below only showcases purego use for macOS and Linux. The other platforms require special handling which can
be seen in the complete example at examples/libc which supports FreeBSD and Windows.
package main
import (
"fmt"
"runtime"
"github.com/bnema/purego"
)
func getSystemLibrary() string {
switch runtime.GOOS {
case "darwin":
return "/usr/lib/libSystem.B.dylib"
case "linux":
return "libc.so.6"
default:
panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
}
}
func main() {
libc, err := purego.Dlopen(getSystemLibrary(), purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
panic(err)
}
var puts func(string)
purego.RegisterLibFunc(&puts, libc, "puts")
puts("Calling C from Go without Cgo!")
}
Then to run: CGO_ENABLED=0 go run main.go
Questions
If you have questions about how to incorporate purego in your project or want to discuss
how it works join the Discord!
External Code
Purego uses code that originates from the Go runtime. These files are under the BSD-3
License that can be found in the Go Source.
This is a list of the copied files:
abi_*.h from package runtime/cgo
wincallback.go from package runtime
zcallback_darwin_*.s from package runtime
internal/fakecgo/abi_*.h from package runtime/cgo
internal/fakecgo/asm_GOARCH.s from package runtime/cgo
internal/fakecgo/callbacks.go from package runtime/cgo
internal/fakecgo/iscgo.go from package runtime/cgo
internal/fakecgo/setenv.go from package runtime/cgo
internal/fakecgo/freebsd.go from package runtime/cgo
internal/fakecgo/netbsd.go from package runtime/cgo
internal/fakecgo/linux.go from package runtime/cgo
The internal/fakecgo/go_GOOS.go files were modified from runtime/cgo/gcc_GOOS_GOARCH.go.
The internal/fakecgo/linux.go file is a combination of runtime/cgo/linux.go and runtime/cgo/linux_syscall.c.
The files abi_*.h and internal/fakecgo/abi_*.h are the same because Bazel does not support cross-package use of
#include so we need each one once per package. (cf. issue)
source
1# purego
2[](https://pkg.go.dev/github.com/bnema/purego?GOOS=darwin)
3
4A library for calling C functions from Go without Cgo.
5
6> Fork notice: this repository is a maintained fork of `github.com/ebitengine/purego` with additional ABI, callback, and generator-facing fixes needed by the PureGoTK stack.
7
8> This is beta software so expect bugs and potentially API breaking changes
9> but each release will be tagged to avoid breaking people's code.
10> Bug reports are encouraged.
11
12## Motivation
13
14The [Ebitengine](https://github.com/hajimehoshi/ebiten) game engine was ported to use only Go on Windows. This enabled
15cross-compiling to Windows from any other operating system simply by setting `GOOS=windows`. The purego project was
16born to bring that same vision to the other platforms supported by Ebitengine.
17
18## Benefits
19
20- **Simple Cross-Compilation**: No C means you can build for other platforms easily without a C compiler.
21- **Faster Compilation**: Efficiently cache your entirely Go builds.
22- **Smaller Binaries**: Using Cgo generates a C wrapper function for each C function called. Purego doesn't!
23- **Dynamic Linking**: Load symbols at runtime and use it as a plugin system.
24- **Foreign Function Interface**: Call into other languages that are compiled into shared objects.
25- **Cgo Fallback**: Works even with CGO_ENABLED=1 so incremental porting is possible.
26This also means unsupported GOARCHs (freebsd/riscv64, linux/mips, etc.) will still work
27except for float arguments and return values.
28
29## Supported Platforms
30
31### Tier 1
32
33Tier 1 platforms are the primary targets officially supported by PureGo. When a new version of PureGo is released, any critical bugs found on Tier 1 platforms are treated as release blockers. The release will be postponed until such issues are resolved.
34
35- **Android**: amd64<sup>1</sup>, arm64<sup>1</sup>
36- **iOS**: amd64<sup>1</sup>, arm64<sup>1</sup>
37- **Linux**: amd64, arm64
38- **macOS**: amd64, arm64
39- **Windows**: amd64, arm64
40
41### Tier 2
42
43Tier 2 platforms are supported by PureGo on a best-effort basis. Critical bugs on Tier 2 platforms do not block new PureGo releases. However, fixes contributed by external contributors are very welcome and encouraged.
44
45- **Android**: 386<sup>1</sup>, arm<sup>1</sup>
46- **FreeBSD**: amd64<sup>2</sup>, arm64<sup>2</sup>
47- **Linux**: 386, arm, loong64, ppc64le, riscv64, s390x<sup>1</sup>
48- **NetBSD**: amd64<sup>2</sup>, arm64<sup>2</sup>
49- **Windows**: 386<sup>3</sup>, arm<sup>3,4</sup>
50
51#### Support Notes
52
531. These architectures require CGO_ENABLED=1 to compile
542. These architectures require the special flag `-gcflags="github.com/bnema/purego/internal/fakecgo=-std"` to compile with CGO_ENABLED=0
553. These architectures only support `SyscallN` and `NewCallback`
564. These architectures are no longer supported as of Go 1.26
57
58## Example
59
60The example below only showcases purego use for macOS and Linux. The other platforms require special handling which can
61be seen in the complete example at [examples/libc](https://github.com/bnema/purego/tree/main/examples/libc) which supports FreeBSD and Windows.
62
63```go
64package main
65
66import (
67 "fmt"
68 "runtime"
69
70 "github.com/bnema/purego"
71)
72
73func getSystemLibrary() string {
74 switch runtime.GOOS {
75 case "darwin":
76 return "/usr/lib/libSystem.B.dylib"
77 case "linux":
78 return "libc.so.6"
79 default:
80 panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
81 }
82}
83
84func main() {
85 libc, err := purego.Dlopen(getSystemLibrary(), purego.RTLD_NOW|purego.RTLD_GLOBAL)
86 if err != nil {
87 panic(err)
88 }
89 var puts func(string)
90 purego.RegisterLibFunc(&puts, libc, "puts")
91 puts("Calling C from Go without Cgo!")
92}
93```
94
95Then to run: `CGO_ENABLED=0 go run main.go`
96
97## Questions
98
99If you have questions about how to incorporate purego in your project or want to discuss
100how it works join the [Discord](https://discord.gg/HzGZVD6BkY)!
101
102### External Code
103
104Purego uses code that originates from the Go runtime. These files are under the BSD-3
105License that can be found [in the Go Source](https://github.com/golang/go/blob/master/LICENSE).
106This is a list of the copied files:
107
108* `abi_*.h` from package `runtime/cgo`
109* `wincallback.go` from package `runtime`
110* `zcallback_darwin_*.s` from package `runtime`
111* `internal/fakecgo/abi_*.h` from package `runtime/cgo`
112* `internal/fakecgo/asm_GOARCH.s` from package `runtime/cgo`
113* `internal/fakecgo/callbacks.go` from package `runtime/cgo`
114* `internal/fakecgo/iscgo.go` from package `runtime/cgo`
115* `internal/fakecgo/setenv.go` from package `runtime/cgo`
116* `internal/fakecgo/freebsd.go` from package `runtime/cgo`
117* `internal/fakecgo/netbsd.go` from package `runtime/cgo`
118* `internal/fakecgo/linux.go` from package `runtime/cgo`
119
120The `internal/fakecgo/go_GOOS.go` files were modified from `runtime/cgo/gcc_GOOS_GOARCH.go`.
121The `internal/fakecgo/linux.go` file is a combination of `runtime/cgo/linux.go` and `runtime/cgo/linux_syscall.c`.
122
123The files `abi_*.h` and `internal/fakecgo/abi_*.h` are the same because Bazel does not support cross-package use of
124`#include` so we need each one once per package. (cf. [issue](https://github.com/bazelbuild/rules_go/issues/3636))