2

这个问题与cross compile OSX to linux 的 go-sqlite3 编译器参数相同,但由于该问题没有答案,我将再次尝试询问。我有一个使用github.com/mattn/go-sqlite3 SQLite3 驱动程序的 Go 包,类似于以下内容:

package main

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

但是,如果我尝试在启用 CGO 的情况下为 Linux AMD64 编译它(根据 SQLite3 的要求),我会收到以下错误:

> env GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build
# github.com/mattn/go-sqlite3
sqlite3-binding.c:33123:42: error: use of undeclared identifier 'pread64'
sqlite3-binding.c:33141:42: error: use of undeclared identifier 'pwrite64'
sqlite3-binding.c:33274:22: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'
sqlite3-binding.c:33283:22: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'
sqlite3-binding.c:33310:20: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'
sqlite3-binding.c:33327:16: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'
sqlite3-binding.c:14220:38: note: expanded from macro 'ArraySize'
sqlite3-binding.c:33331:14: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'
sqlite3-binding.c:14220:38: note: expanded from macro 'ArraySize'
sqlite3-binding.c:35984:11: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
sqlite3-binding.c:33127:49: note: expanded from macro 'osPread64'
sqlite3-binding.c:36096:17: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
sqlite3-binding.c:33145:57: note: expanded from macro 'osPwrite64'

同样,如果我按照 https://github.com/mattn/go-sqlite3#linux 中的说明进行指定--tags "linux"会收到以下错误:

> go build --tags "linux"
# runtime/internal/sys
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:8:7: GOOS redeclared in this block
    previous declaration at /usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_darwin.go:7:14
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:10:7: GoosAix redeclared in this block
    previous declaration at /usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_darwin.go:9:17
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:11:7: GoosAndroid redeclared in this block
    previous declaration at /usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_darwin.go:10:21
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:12:7: GoosDarwin redeclared in this block
    previous declaration at /usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_darwin.go:11:20
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:13:7: GoosDragonfly redeclared in this block
    previous declaration at /usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_darwin.go:12:23
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:14:7: GoosFreebsd redeclared in this block
    previous declaration at /usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_darwin.go:13:21
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:15:7: GoosHurd redeclared in this block
    previous declaration at /usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_darwin.go:14:18
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:16:7: GoosJs redeclared in this block
    previous declaration at /usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_darwin.go:15:16
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:17:7: GoosLinux redeclared in this block
    previous declaration at /usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_darwin.go:16:19
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:18:7: GoosNacl redeclared in this block
    previous declaration at /usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_darwin.go:17:18
/usr/local/Cellar/go@1.12/1.12.12/libexec/src/runtime/internal/sys/zgoos_linux.go:18:7: too many errors
# runtime/cgo
duplicate symbol '__cgo_sys_thread_start' in:
    $WORK/b040/_x004.o
    $WORK/b040/_x007.o
duplicate symbol '_x_cgo_init' in:
    $WORK/b040/_x004.o
    $WORK/b040/_x007.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

如何在我的 MacOS (Darwin AMD64) 笔记本电脑上交叉编译此包以在 Google Cloud Linux VM 上运行?

4

1 回答 1

6

https://github.com/mattn/go-sqlite3/issues/384#issuecomment-433584967可以看出,这可以通过使用musl-cross交叉编译器工具链来解决:

brew install FiloSottile/musl-cross/musl-cross

其次是

> env CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++ GOARCH=amd64 GOOS=linux CGO_ENABLED=1 \
  go build -ldflags "-linkmode external -extldflags -static"
于 2020-01-12T01:09:31.843 回答