mattn/go-sqlite3

cross compile error from linux to mac

sangchul opened this issue · 2 comments

OS: CentOS Linux release 7.4.1708 (Core)
Go: go version go1.18.1 linux/amd64

my source code

import (
   ...
   sqlite3 "github.com/mattn/go-sqlite3"
   ...
)

func isUniqueViolationError(err error) bool {
    if sqlErr, ok := err.(sqlite3.Error); ok {
        return sqlErr.ExtendedCode == sqlite3.ErrConstraintUnique
    }
    return false
}

I got following build error if I cross-compile for darwin/arm64

$ GOOS=darwin GOARCH=arm64 go build -ldflags "$GO_LDFLAGS"
../../dao/sqlite_dao/dbclient.go:287:32: undefined: sqlite3.Error
../../dao/sqlite_dao/dbclient.go:288:41: undefined: sqlite3.ErrConstraintUnique

but if I tried to build it with option linux/amd64, it builds well.

If you need other information, please let me know.
Thanks in advance

I tried the compilation on my apple silicon environment to build linux binary, it also fails with above error. I don't understand why go compiler is not able to follow sqlite3 module in cross compilation case

To cross-compile any cgo library, you must set CGO_ENABLED=1. (By default the Go compiler enables cgo for native builds but not cross compilation.) You will also need to set CC to an applicable C cross-compiler.