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 comments 到 CL 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学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习