如何使用github.com/mattn/go-sqlite3对Google Cloud VM交叉编译Go软件包?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用github.com/mattn/go-sqlite3对Google Cloud VM交叉编译Go软件包?相关的知识,希望对你有一定的参考价值。

此问题与go-sqlite3 compiler arguments for cross compile OSX to linux相同,但是由于没有答案,我将尝试再次询问。我有一个Go包,它使用github.com/mattn/go-sqlite3 SQLite3驱动程序,类似于以下内容:

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'

类似地,如果我按照--tags "linux"的说明指定了https://github.com/mattn/go-sqlite3#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上运行?

答案

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"

以上是关于如何使用github.com/mattn/go-sqlite3对Google Cloud VM交叉编译Go软件包?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用本机反应创建登录以及如何验证会话

如何在自动布局中使用约束标识符以及如何使用标识符更改约束? [迅速]

如何使用 AngularJS 的 ng-model 创建一个数组以及如何使用 jquery 提交?

如何使用laravel保存所有行数据每个行名或相等

如何使用 Math.Net 连接矩阵。如何使用 Math.Net 调用特定的行或列?

WSARecv 如何使用 lpOverlapped?如何手动发出事件信号?