Go编译为windows/arm和arm64,使用buildmode=c-shared
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学习网公众号!

- 上一篇
- etcd-go包之间的区别是什么?

- 下一篇
- Go gin 中如何获取请求的主机名?
-
- Golang · Go问答 | 1年前 |
- 在读取缓冲通道中的内容之前退出
- 139浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 戈兰岛的全球 GOPRIVATE 设置
- 204浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将结构作为参数传递给 xml-rpc
- 325浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何用golang获得小数点以下两位长度?
- 477浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何通过 client-go 和 golang 检索 Kubernetes 指标
- 486浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将多个“参数”映射到单个可变参数的习惯用法
- 439浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将 HTTP 响应正文写入文件后出现 EOF 错误
- 357浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 结构中映射的匿名列表的“复合文字中缺少类型”
- 352浏览 收藏
-
- Golang · Go问答 | 1年前 |
- NATS Jetstream 的性能
- 101浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将复杂的字符串输入转换为mapstring?
- 440浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 相当于GoLang中Java将Object作为方法参数传递
- 212浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何确保所有 goroutine 在没有 time.Sleep 的情况下终止?
- 143浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 魔匠AI
- SEO摘要魔匠AI专注于高质量AI学术写作,已稳定运行6年。提供无限改稿、选题优化、大纲生成、多语言支持、真实参考文献、数据图表生成、查重降重等全流程服务,确保论文质量与隐私安全。适用于专科、本科、硕士学生及研究者,满足多语言学术需求。
- 20次使用
-
- PPTFake答辩PPT生成器
- PPTFake答辩PPT生成器,专为答辩准备设计,极致高效生成PPT与自述稿。智能解析内容,提供多样模板,数据可视化,贴心配套服务,灵活自主编辑,降低制作门槛,适用于各类答辩场景。
- 36次使用
-
- Lovart
- SEO摘要探索Lovart AI,这款专注于设计领域的AI智能体,通过多模态模型集成和智能任务拆解,实现全链路设计自动化。无论是品牌全案设计、广告与视频制作,还是文创内容创作,Lovart AI都能满足您的需求,提升设计效率,降低成本。
- 39次使用
-
- 美图AI抠图
- 美图AI抠图,依托CVPR 2024竞赛亚军技术,提供顶尖的图像处理解决方案。适用于证件照、商品、毛发等多场景,支持批量处理,3秒出图,零PS基础也能轻松操作,满足个人与商业需求。
- 45次使用
-
- PetGPT
- SEO摘要PetGPT 是一款基于 Python 和 PyQt 开发的智能桌面宠物程序,集成了 OpenAI 的 GPT 模型,提供上下文感知对话和主动聊天功能。用户可高度自定义宠物的外观和行为,支持插件热更新和二次开发。适用于需要陪伴和效率辅助的办公族、学生及 AI 技术爱好者。
- 44次使用
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- Golang取得代码运行时间的问题
- 2023-02-24 501浏览
-
- 请问 go 代码如何实现在代码改动后不需要Ctrl+c,然后重新 go run *.go 文件?
- 2023-01-08 501浏览
-
- 如何从同一个 io.Reader 读取多次
- 2023-04-11 501浏览