未定义:尝试为armv7构建go应用程序时,SQLiteConn

I have to compile a Go service for Ubuntu ARM-v7

When I compile it with

GOARCH=arm GOARM=7 go build -v -o release/edge_to_bc -ldflags '-s -w -extldflags "-static"' ./...

I get:

gitlab.com/company/edge_to_bc/vendor/github.com/hyperledger/fabric/bccsp/pkcs11
# gitlab.com/company/edge_to_bc/vendor/github.com/hyperledger/fabric/bccsp/pkcs11
vendor/github.com/hyperledger/fabric/bccsp/pkcs11/impl.go:82:12: undefined: pkcs11.Ctx
vendor/github.com/hyperledger/fabric/bccsp/pkcs11/impl.go:83:16: undefined: pkcs11.SessionHandle
gitlab.com/company/edge_to_bc/vendor/github.com/mattn/go-sqlite3
# gitlab.com/company/edge_to_bc/vendor/github.com/mattn/go-sqlite3
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:18:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:26:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:27:17: undefined: namedValue
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:29:13: undefined: namedValue
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:35:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:44:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:49:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:54:10: undefined: SQLiteStmt
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:63:10: undefined: SQLiteStmt
vendor/github.com/mattn/go-sqlite3/tracecallback_noimpl.go:8:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:29:13: too many errors

How should I fix it ?

Go-sqlite3 is cgo package.

If you want to build your app using go-sqlite3, you need gcc.

However, after you have built and installed go-sqlite3 with go install github.com/mattn/go-sqlite3 (which requires gcc), you can build your app without relying on gcc in future.

Important: because this is a CGO enabled package you are required to set the environment variable CGO_ENABLED=1 and have a gcc compile present within your path.

Source

Cross compiling from Ubuntu to ARM7:

sudo apt install \
 libc6-armel-cross \
 libc6-dev-armel-cross \
 binutils-arm-linux-gnueabi \
 libncurses5-dev \
 gcc-arm-linux-gnueabihf

env CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ \
    CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 \
    go build -v 

Using a docker image:

# Install docker
$ curl -fsSL https://get.docker.com -o get-docker.sh | sudo sh -
$ sudo usermod -aG docker your-user

# Go to your project folder
$ cd your-project-folder

# Compile
$ docker run --rm \
    -v /tmp/.docker/go:/go \
    -v /tmp/.docker/go-build:/root/.cache/go-build \
    -v $PWD:$PWD \
    -w $PWD \
    filipeandre/go-compiler-to-arm7:1.12 \
    go build -v

Dockerfile