Generate libm bridge for resolving undefined symbol in cgo
( Put same example in _example directory )
$ tree
.
├── ccall
│ ├── ccall.c
│ └── ccall.go
├── go.mod
├── go.sum
└── main.go
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);
}$ 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'
2Produce warning: implicitly declaring because this example doesn't link libm .
go get github.com/goccy/cgo-math/cmd/cgo-math-bindgen$ cgo-math-bindgen --package ccall --output ccallGenerate bridge source to ccall/bridge_math.go as ccall package
$ go run main.go
2Yeah ! we resolved reference to sqrt symbol !