我正在学习Rust并编写一些基本的CLI工具作为练习。我将我的应用程序源代码存储在Github中,使用Github操作生成二进制文件并通过Github发布发布这些二进制文件。
问题是;我不确定如何针对各种目标架构和操作系统交叉编译Rust应用程序。
(为比较道歉)以前使用Go时,我可以在build命令中指定目标CPU架构和目标操作系统,如:
env GOARCH=arm64 GOOS=darwin go build
当我想看看Rust中是否有等价的东西时,我看到了一些说明,告诉我使用虚拟化和其他各种技术来交叉编译。
我怀疑我可能只是不擅长研究,有没有一种等价的简单方法来交叉编译Rust应用程序?
如果没有,为什么会这样,你能给我指一些资源来帮助我学习如何做到这一点吗?
2条答案
按热度按时间qv7cva1a1#
cross
使这变得非常容易,特别是因为它得到了actions-rs/cargo
的支持。我用的是
在一个my project上。([编辑:] Improved version with autopublishing)
我还特别指出
在我的
Cargo.toml
使发布的文件更好一点。可能值得注意的是,Rust crates使得在C/C++库中构建和链接比Go更容易,可能会调用CMake或更糟。交叉编译这样的crate可能要困难得多,具体如何做取决于具体的crate。
7bsow1i62#
对于一次性测试,或者在https://godbolt.org/上运行,可以使用
--target=
,类似于clang -target
。例如,Godbolt使用**
-O --target=aarch64-apple-darwin
而不是默认的x86_64-unknown-linux-gnu
。(我猜x86_64有第四个组件,就像x86一样,对于Windows可以是pc
,对于非Windows可以是unknown
。Godbolt的“compile to binary”选项在“output”中没有给出输出,可能是因为它仍然使用x86-64
objdump
而不是llvm-objdump
,后者可以自动检测目标文件的体系结构。如果你想测试不同架构的内联asm如何编译,这甚至可以用在
use std::arch::asm;
上。要查看可用的目标,请执行
--print target-list
**。(你可以在Godbolt上这样做,只要看看编译器的输出窗格。