| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors |
| 3 | |
| 4 | //go:build darwin || freebsd || linux || netbsd || windows |
| 5 | |
| 6 | package mainscip-go gomod github.com/ebitengine/purego . `github.com/ebitengine/purego/examples/libc`/ |
| 7 | |
| 8 | import ( |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 10 | "runtimescip-go gomod github.com/golang/go/src go1.18 runtime/" |
| 11 | |
| 12 | "github.com/ebitengine/puregoscip-go gomod github.com/ebitengine/purego . `github.com/ebitengine/purego`/" |
| 13 | ) |
| 14 | |
| 15 | func getSystemLibraryscip-go gomod github.com/ebitengine/purego . `github.com/ebitengine/purego/examples/libc`/getSystemLibrary().() string { |
| 16 | switch runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.18 runtime/GOOS. { |
| 17 | case "darwin": |
| 18 | return "/usr/lib/libSystem.B.dylib" |
| 19 | case "freebsd": |
| 20 | return "libc.so.7" |
| 21 | case "linux": |
| 22 | return "libc.so.6" |
| 23 | case "netbsd": |
| 24 | return "libc.so" |
| 25 | case "windows": |
| 26 | return "ucrtbase.dll" |
| 27 | default: |
| 28 | panic(fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("GOOS=%s is not supported", runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.18 runtime/GOOS.)) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func mainscip-go gomod github.com/ebitengine/purego . `github.com/ebitengine/purego/examples/libc`/main().() { |
| 33 | libc, err := openLibraryscip-go gomod github.com/ebitengine/purego . `github.com/ebitengine/purego/examples/libc`/openLibrary().(getSystemLibraryscip-go gomod github.com/ebitengine/purego . `github.com/ebitengine/purego/examples/libc`/getSystemLibrary().()) |
| 34 | if err != nil { |
| 35 | panic(err) |
| 36 | } |
| 37 | var puts func(string) |
| 38 | puregoscip-go gomod github.com/ebitengine/purego . `github.com/ebitengine/purego`/.RegisterLibFuncscip-go gomod github.com/ebitengine/purego . `github.com/ebitengine/purego`/RegisterLibFunc().(&puts, libc, "puts") |
| 39 | puts("Calling C from Go without Cgo!") |
| 40 | } |