goccy / cgo-math

Generate libm bridge for resolving undefined symbol in cgo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cgo-math

Generate libm bridge for resolving undefined symbol in cgo

Example

( Put same example in _example directory )

Working Tree

$ tree
.
├── ccall
│   ├── ccall.c
│   └── ccall.go
├── go.mod
├── go.sum
└── main.go

Contents

  • go.mod
module example

go 1.12
  • main.go

Only call ccall.Sqrt(float64)float64 .

package main

import (
	"example/ccall"
	"fmt"
)

func main() {
	fmt.Println(ccall.Sqrt(4))
}
  • ccall/ccall.go

define Sqrt(float64)float64 and call C API ( double callSqrt(double x) )

package ccall

// extern double callSqrt(double x);
import "C"

func Sqrt(x float64) float64 {
	return float64(C.callSqrt(C.double(x)))
}
  • ccall/ccall.c

define double callSqrt(double) and call sqrt

#include "_cgo_export.h"

double callSqrt(double x) {
  return sqrt(x);
}

Run example

$ go run main.go
# example/ccall
ccall.c:4:10: warning: implicitly declaring library function 'sqrt' with type 'double (double)' [-Wimplicit-function-declaration]
ccall.c:4:10: note: include the header <math.h> or explicitly provide a declaration for 'sqrt'
2

Produce warning: implicitly declaring because this example doesn't link libm .

Executes cgo-math-bindgen

Install CLI

go get github.com/goccy/cgo-math/cmd/cgo-math-bindgen

Run generator

$ cgo-math-bindgen --package ccall --output ccall

Generate bridge source to ccall/bridge_math.go as ccall package

Try to run example again

$ go run main.go
2

Yeah ! we resolved reference to sqrt symbol !

About

Generate libm bridge for resolving undefined symbol in cgo


Languages

Language:Go 99.0%Language:C 1.0%