cgo only supports integer-sized parameters
// native.h
typedef struct Vector2
{
float x;
float y;
} Vector2;
Vector2 Vector2Add(Vector2 v1, Vector2 v2)
{
Vector2 result = {v1.x + v2.x, v1.y + v2.y};
return result;
}
// main.go
package main
// #include "native.h"
import "C"
import "fmt"
func main() {
a := C.Vector2{1, 2}
b := C.Vector2{1, 2}
c := C.Vector2Add(a, b)
fmt.Println(c)
}
> go run main.go
{2 4}
> tinygo run main.go
{3 2}
> tinygo version
tinygo version 0.33.0 darwin/amd64 (using go version go1.23.2 and LLVM version 18.1.2)
Windows:
> tinygo.exe run main.go
{0 0}
> go run main.go
{2 4}
Ugh, this has got to be calling convention differences for non-integer-sized objects.
Oh, cgo in tinygo supports only integer-sized objects - its not issue, nevermind then.
Reopening because it's still a cgo bug/limitation.
@dgryski any workaround for this issue? Can I compile c-code as part of go-project to wasm-browser target with tinygo? Standard go-compiler doesnt support it.
Matching the C ABI is a difficult problem, since this is basically an implementation detail between Clang and LLVM. There have been various proposals to expose this as a library, the most recent being https://discourse.llvm.org/t/rfc-an-abi-lowering-library-for-llvm/84495 (and https://github.com/llvm/llvm-project/pull/158329).
I hope one of these proposals will eventually be usable so we can finally fix this.