登录
首页 >  Golang >  Go问答

Go编译为windows/arm和arm64,使用buildmode=c-shared

来源:stackoverflow

时间:2024-02-15 15:24:24 268浏览 收藏

Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《Go编译为windows/arm和arm64,使用buildmode=c-shared》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!


问题内容

我需要通过 c 将 go 库编译为可以通过 arm/arm64 windows 上的 pinvoke 使用的 dll。我发现了许多与此相关的悬而未决的问题、主题和讨论,听起来它可能部分有效。但如果我尝试这样:

export cc="arm-none-eabi-gcc"
    export cxx="arm-none-eabi-g++"
    export goos="windows"
    export goarch="arm"
    export goarm=7
    export cgo_enabled="1"
    go build -ldflags="-s -w" -o my_library.dll -buildmode c-shared

我得到的结果是 windows/arm 不支持 buildmode c-shares。所以好像还是不支持。

另一个问题是我需要设置 cgo_enabled 并通过 c/c++ 工具链进行编译,因为我必须添加由 swig 生成的 c 文件。我在 ubunu 20.04 上使用 gcc-arm-none-eabi 包的工具链尝试了上述操作。

我不是 c/c++/go 专业人士 - 但同样适用于几乎所有其他平台,如 windows、linux、android、mac 和 ios。后者也是基于 arm64 的,所以我不太明白为什么这是不可能的 - 尽管我很重视这一切的困难。

所以,如果有更深入知识的人可以帮助我,那就太好了。

澄清一下:我不想/不需要为 arm/arm64 编译 go 本身。我需要为该平台编译一个 go 程序(以便在 surface 或 hololens 等上使用 .net 中的库)。

自 2021 年 8 月 4 日更新:

go 1.17rc2 现在应该包含 windows arm64。我得到了使用 zig 进行交叉编译的提示。因此,我将构建管道更改为如下所示(我在 ubuntu vm 中使用 azure devops):

go get -v golang.org/dl/go1.17rc2
/home/vsts/go/bin/go1.17rc2 download
/home/vsts/go/bin/go1.17rc2 version
sudo snap install zig --classic --beta
zig version
export cc="zig cc -target aarch64-windows-gnu"
export cxx="zig c++ -target aarch64-windows-gnu"
export goos="windows"
export goarch="arm64"
export goarm=7
export cgo_enabled="1"
/home/vsts/go/bin/go1.17rc2 build -ldflags="-s -w" -o storj_uplink.dll -buildmode c-shared -tags extended

然后我收到此错误:

2021-08-03t19:24:52.0641737z # runtime/cgo
2021-08-03t19:24:52.0642803z info: usage: zig [command] [options]
2021-08-03t19:24:52.0643335z 
2021-08-03t19:24:52.0643827z commands:
2021-08-03t19:24:52.0643940z 
2021-08-03t19:24:52.0644276z   build            build project from build.zig
2021-08-03t19:24:52.0645203z   init-exe         initialize a `zig build` application in the cwd
2021-08-03t19:24:52.0645768z   init-lib         initialize a `zig build` library in the cwd
2021-08-03t19:24:52.0645950z 
2021-08-03t19:24:52.0646407z   ast-check        look for simple compile errors in any set of files
2021-08-03t19:24:52.0646936z   build-exe        create executable from source or object files
2021-08-03t19:24:52.0647468z   build-lib        create library from source or object files
2021-08-03t19:24:52.0647994z   build-obj        create object from source or object files
2021-08-03t19:24:52.0648390z   fmt              reformat zig source into canonical form
2021-08-03t19:24:52.0648753z   run              create executable and run immediately
2021-08-03t19:24:52.0649088z   test             create and run a test build
2021-08-03t19:24:52.0649551z   translate-c      convert c code to zig code
2021-08-03t19:24:52.0649707z 
2021-08-03t19:24:52.0650109z   ar               use zig as a drop-in archiver
2021-08-03t19:24:52.0650576z   cc               use zig as a drop-in c compiler
2021-08-03t19:24:52.0651070z   c++              use zig as a drop-in c++ compiler
2021-08-03t19:24:52.0651549z   dlltool          use zig as a drop-in dlltool.exe
2021-08-03t19:24:52.0652033z   lib              use zig as a drop-in lib.exe
2021-08-03t19:24:52.0652495z   ranlib           use zig as a drop-in ranlib
2021-08-03t19:24:52.0652670z 
2021-08-03t19:24:52.0652962z   env              print lib path, std path, cache directory, and version
2021-08-03t19:24:52.0653531z   help             print this help and exit
2021-08-03t19:24:52.0653879z   libc             display native libc paths file or validate one
2021-08-03t19:24:52.0654250z   targets          list available compilation targets
2021-08-03t19:24:52.0654579z   version          print version number and exit
2021-08-03t19:24:52.0655062z   zen              print zen of zig and exit
2021-08-03t19:24:52.0655220z 
2021-08-03t19:24:52.0655445z general options:
2021-08-03t19:24:52.0655565z 
2021-08-03t19:24:52.0655982z   -h, --help       print command-specific usage
2021-08-03t19:24:52.0656154z 
2021-08-03t19:24:52.0656502z error: unknown command: -e
2021-08-03t19:25:03.2047129z # golang.org/x/sys/windows
2021-08-03t19:25:03.2048568z /home/vsts/go/pkg/mod/golang.org/x/[email protected]/windows/types_windows.go:1620:24: undefined: jobobject_basic_limit_information
2021-08-03t19:25:03.2049594z /home/vsts/go/pkg/mod/golang.org/x/[email protected]/windows/zsyscall_windows.go:3020:38: undefined: wsadata
2021-08-03t19:25:03.2050606z /home/vsts/go/pkg/mod/golang.org/x/[email protected]/windows/zsyscall_windows.go:3096:51: undefined: servent
2021-08-03t19:25:03.2051572z /home/vsts/go/pkg/mod/golang.org/x/[email protected]/windows/zsyscall_windows.go:3110:50: undefined: servent
2021-08-03t19:25:04.7947309z ##[error]bash exited with code '1'.

基本上是“未知命令:-e”,此处描述。但根据我的理解,这应该已经有效了。此外,这篇博文也直接使用 zig 来实现。

2021 年 8 月 4 日第二次更新

go 现在正在调用 zig!使用 bash 脚本的解决方法有效。现在我收到以下错误:

2021-08-04t11:54:47.2530981z # golang.org/x/sys/windows
2021-08-04t11:54:47.2532284z /home/vsts/go/pkg/mod/golang.org/x/[email protected]/windows/types_windows.go:1620:24: undefined: jobobject_basic_limit_information
2021-08-04t11:54:47.2533180z /home/vsts/go/pkg/mod/golang.org/x/[email protected]/windows/zsyscall_windows.go:3020:38: undefined: wsadata
2021-08-04t11:54:47.2534002z /home/vsts/go/pkg/mod/golang.org/x/[email protected]/windows/zsyscall_windows.go:3096:51: undefined: servent
2021-08-04t11:54:47.2534797z /home/vsts/go/pkg/mod/golang.org/x/[email protected]/windows/zsyscall_windows.go:3110:50: undefined: servent
2021-08-04t11:54:57.4223210z # runtime/cgo
2021-08-04t11:54:57.4224911z /snap/zig/3678/lib/libc/mingw/secapi/vsprintf_s.c:39:10: warning: implicit declaration of function '__ms_vsnprintf' is invalid in c99 [-wimplicit-function-declaration]
2021-08-04t11:54:57.4225714z   return __ms_vsnprintf (_dstbuf, _size, _format, _arglist);
2021-08-04t11:54:57.4226223z          ^
2021-08-04t11:54:57.4226624z 1 warning generated.
2021-08-04t11:54:57.4227534z /snap/zig/3678/lib/libc/mingw/math/arm/s_trunc.c/snap/zig/3678/lib/libc/mingw/math/arm/s_truncf.c:24:10: fatal error: '../bsd_private_base.h' file not found
2021-08-04t11:54:57.4228188z #include "../bsd_private_base.h"
2021-08-04t11:54:57.4228651z          ^~~~~~~~~~~~~~~~~~~~~~~
2021-08-04t11:54:57.4229332z :26:10: fatal error: '../bsd_private_base.h' file not found
2021-08-04t11:54:57.4229850z #include "../bsd_private_base.h"
2021-08-04t11:54:57.4230310z          ^~~~~~~~~~~~~~~~~~~~~~~
2021-08-04t11:54:57.4230966z 1 error generated.
2021-08-04t11:54:57.4231397z 1 error generated.
2021-08-04t11:54:57.4522549z ##[error]bash exited with code '1'.

自 2021 年 8 月 5 日更新:

我终于找到了一个至少不会抛出错误的工具链。但现在它静静地退出,不生成 dll。但不确定现在会发生什么。这是我的电话:

go get -v golang.org/dl/go1.17rc2
/home/vsts/go/bin/go1.17rc2 download
/home/vsts/go/bin/go1.17rc2 version
wget https://developer.arm.com/-/media/Files/downloads/gnu-a/10.3-2021.07/binrel/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf.tar.xz
tar -xf gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf.tar.xz
cd gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf
cd bin
export PATH=$PATH:$(pwd)
cd ..
cd ..
cd uplink-c

export CC="arm-none-linux-gnueabihf-gcc -v"
export CXX="arm-none-linux-gnueabihf-g++ -v"
export GOOS="windows"
export GOARCH="arm64"
export GOARM=7
export CGO_ENABLED="1"
home/vsts/go/bin/go1.17rc2 build -ldflags="-s -w" -o storj_uplink.dll -buildmode c-shared -tags extended -v

解决方案


应该可以,使用 Go 1.17 beta

Its documentation 请提及:

OP topperdel 指的是 the commentsCL 326310

正如这些问题所示,这仍然是一项正在进行的工作:

  • issue 46502(“runtime:由于重新声明警告(升级为测试中的错误),windows-arm64-aws 构建器上的多项测试失败”),
  • issue 46701(“arm64/arm 上的 Powershell 无法加载 arm64/arm 二进制文件,因为它是英特尔进程”)

测试部分现已(2021 年 6 月 13 日)结束,编号为 golang/go commit 1ed0d12

我在the comments中提到

ziglang/zig issue 7342 开始,现在应该支持 zig。
以 Go 1.17 为例:来自 Loris Cro 的“Zig Makes Go Cross Compilation Just Work”(社区副总裁 @ziglang):

您需要根据您的需要调整架构目标

注意:需要 bash shell 会话。例如,Windows 上的 git bash。

以上就是《Go编译为windows/arm和arm64,使用buildmode=c-shared》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>