asg017 / sqlite-vec

A vector search SQLite extension that runs anywhere!
Apache License 2.0
4.3k stars 136 forks source link

undefined reference to `sqrt' #84

Closed fayfive closed 3 months ago

fayfive commented 3 months ago

I compile examples/simple-go-cgo/demo.go with command go build Here is go env

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/alfred/.cache/go-build'
GOENV='/home/alfred/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/alfred/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/alfred/go'
GOPRIVATE=''
GOPROXY='https://goproxy.io,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.6'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/alfred/working/go/clip_go/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build532488598=/tmp/go-build -gno-record-gcc-switches'

My system is ubuntu 22.04 go version is 1.22.6 I find the include file /usr/include/math.h the lib /usr/lib/x86_64-linux-gnu/libm.so Thanks!

BeeBreeze commented 3 months ago

In makefile:143, move -lm to the end of the command

fayfive commented 3 months ago

Hello,I don't want to compile the lib, I just want test, I enter the directory example/simple-go-cgo and 'make' and show that

go build -o demo
# github.com/asg017/sqlite-vec/examples/go
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/bin/ld: /tmp/go-link-3424282799/000002.o: in function `distance_cosine_float':
/home/alfred/go/pkg/mod/github.com/asg017/sqlite-vec-go-bindings@v0.1.1/cgo/sqlite-vec.c:464: undefined reference to `sqrt'
/usr/bin/ld: /home/alfred/go/pkg/mod/github.com/asg017/sqlite-vec-go-bindings@v0.1.1/cgo/sqlite-vec.c:464: undefined reference to `sqrt'
/usr/bin/ld: /tmp/go-link-3424282799/000002.o: in function `distance_cosine_int8':
/home/alfred/go/pkg/mod/github.com/asg017/sqlite-vec-go-bindings@v0.1.1/cgo/sqlite-vec.c:482: undefined reference to `sqrt'
/usr/bin/ld: /home/alfred/go/pkg/mod/github.com/asg017/sqlite-vec-go-bindings@v0.1.1/cgo/sqlite-vec.c:482: undefined reference to `sqrt'
/usr/bin/ld: /tmp/go-link-3424282799/000002.o: in function `vec_normalize':
/home/alfred/go/pkg/mod/github.com/asg017/sqlite-vec-go-bindings@v0.1.1/cgo/sqlite-vec.c:1747: undefined reference to `sqrtf'
/usr/bin/ld: /tmp/go-link-3424282799/000002.o: in function `l2_sqr_int8':
/home/alfred/go/pkg/mod/github.com/asg017/sqlite-vec-go-bindings@v0.1.1/cgo/sqlite-vec.c:374: undefined reference to `sqrtf'
/usr/bin/ld: /tmp/go-link-3424282799/000002.o: in function `l2_sqr_float':
/home/alfred/go/pkg/mod/github.com/asg017/sqlite-vec-go-bindings@v0.1.1/cgo/sqlite-vec.c:359: undefined reference to `sqrtf'
/usr/bin/ld: /tmp/go-link-3424282799/000002.o: in function `l2_sqr_int8':
/home/alfred/go/pkg/mod/github.com/asg017/sqlite-vec-go-bindings@v0.1.1/cgo/sqlite-vec.c:374: undefined reference to `sqrtf'

I upgrade the example/simple-go-cgo/go.mod

module github.com/asg017/sqlite-vec/examples/go

go 1.22.5

require github.com/mattn/go-sqlite3 v1.14.22

require github.com/asg017/sqlite-vec-go-bindings v0.1.1

And I found that go/pkg/mod/github.com/asg017/sqlite-vec-go-bindings@v0.1.1/cgo/lib.go

// #cgo CFLAGS: -DSQLITE_CORE
// #include "sqlite-vec.h"

It seems that there is no lib and the directory similarly like this

 // #cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu
// #cgo LDFLAGS: -ldl -lm

Can you show me how to compile the sample? My system is ubuntu 22.04 go version is 1.22.6 I find the include file /usr/include/math.h the lib /usr/lib/x86_64-linux-gnu/libm.so Thanks!

asg017 commented 3 months ago

@fayfive can you try again with the latest v0.1.2-alpha.7 verision? I added -lm to cgo LDFLAGS for linux environments, hopefully that fixes it

fayfive commented 3 months ago

Yes, it works well.