gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:14:43 28 4
gpt4 key购买 nike

这个问题与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'

同样,如果按照 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上运行?

最佳答案

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"

关于go - 如何使用github.com/mattn/go-sqlite3为Google Cloud VM交叉编译Go软件包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59698483/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com