首页 > 系统相关 >Rust 交叉编译 macOS 为 Linux 和 Windows

Rust 交叉编译 macOS 为 Linux 和 Windows

时间:2024-02-29 14:16:11浏览次数:20  
标签:macOS x86 musl Windows 编译 64 Linux target

目录

前言

鉴于 rust 中文资料较少,遇到问题的解决方案更少。这里记录遇到的一些问题。

Rust 支持交叉编译,可以在 macOS 平台编译出 Linux 或者 Windows 可运行的程序,或者在 Linux 平台编译 macOS 或者 Windows 可运行的程序。

本文主要文章讲解Mac平台编译为其他平台的二进制程序。

想要实现跨平台编译且可运行的程序,那么我们就需要静态链接,这样生成程序才不会因为动态链接库的原因运行失败。

默认情况下,Rust 静态连接所有 Rust 代码。如果程序中使用了标准库,Rust 会连接到系统的libc实现。

环境

苹果系统:
操作系统:macOS 12.3.1 21E258 x86_64
生锈:rustc 1.60.0 (7737e0b5c 2022-04-04)
生锈:rustup 1.24.3 (ce5817a94 2021-05-31)

Linux:
操作系统:EndeavourOS Linux x86_64
核心:5.17.1-arch1-1
生锈:rustc 1.60.0 (7737e0b5c 2022-04-04)
生锈:rustup 1.24.3 (ce5817a94 2021-05-31)

首先需要安装Rust,使用命令`` 。

案例

使用 Cargo 新建二进制项目:

cargo new --bin hello

文件main.rs:

fn main() {
    println!("Hello World!\n");
}

macOS 编译为 Linux 和 Windows 可用二进制程序

编译为 Linux 平台

想要实现Linux平台可以运行的程序,那么就需要使用musl来替代glibc,musl实现了Linux libc。

musl 在macOS上使用musl-cross,musl-cross是专门编译到Linux的工具链,下面进行安装:

musl https://musl.libc.org/

$ brew install FiloSottile/musl-cross/musl-cross

还需要创建musl-gcc:

$ ln -s /usr/local/bin/x86_64-linux-musl-gcc /usr/local/bin/musl-gcc

添加对应的Target,只需要执行一次就可以了:

rustup target add x86_64-unknown-linux-musl

修改配置文件~/.cargo/config(如果没有可以新建),添加以下内容:

[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"

也可以在项目根目录下创建 .cargo/config 文件,只对当前项目生效

# 使用
cargo build --release --target x86_64-unknown-linux-musl

结果:

$ tree -L 2 target/x86_64-unknown-linux-musl 
target/x86_64-unknown-linux-musl
├── CACHEDIR.TAG
└── debug
    ├── build
    ├── deps
    ├── examples
    ├── hello
    ├── hello.d
    └── incremental

5 directories, 3 files
$ file target/x86_64-unknown-linux-musl/debug/hello
target/x86_64-unknown-linux-musl/debug/hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), static-pie linked, with debug_info, not stripped

编译为Windows平台

mingw-w64是用来编译到Windows的工具链,使用如下命令进行安装:

brew install mingw-w64

添加接下来mingw-64的Target,只需要执行一次就可以了:

$ rustup target add x86_64-pc-windows-gnu

修改配置文件~/.cargo/config(如果没有可以新建),设置Linker,添加如下内容:

[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
ar = "x86_64-w64-mingw32-gcc-ar"

# 使用
$ cargo build --release --target x86_64-unknown-linux-musl

结果:

$ tree -L 2 target/x86_64-pc-windows-gnu
target/x86_64-pc-windows-gnu
├── CACHEDIR.TAG
└── debug
    ├── build
    ├── deps
    ├── examples
    ├── hello.d
    ├── hello.exe
    └── incremental

5 directories, 3 files
$ file target/x86_64-pc-windows-gnu/debug/hello.exe
target/x86_64-pc-windows-gnu/debug/hello.exe: PE32+ executable (console) x86-64, for MS Windows

最后

- https://tomshine.hashnode.dev/rust-macos-linux-windows

rust合集


标签:macOS,x86,musl,Windows,编译,64,Linux,target
From: https://www.cnblogs.com/JavaPub/p/18043572

相关文章

  • linux服务文件存放位置
    转自:https://wenku.csdn.net/answer/d563a2b1b3f3c4e717cadb694b160ed4Linux中的service文件是一种用于管理系统服务的配置文件,通常位于/etc/systemd/system目录下。这些文件包含了服务的启动、停止、重启等操作的指令,以及服务的相关配置信息,如服务的名称、描述、依赖关系等。......
  • VMware Workstation 17.5.1 Pro for Windows & Linux - 适用于 Windows 和 Linux 的桌
    VMwareWorkstation17.5.1ProforWindows&Linux-适用于Windows和Linux的桌面虚拟化软件基于x86的Windows、Linux桌面虚拟化软件请访问原文链接:https://sysin.org/blog/vmware-workstation-17/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org桌面Hype......
  • Linux_时间同步和开机自启动
    时间同步时间同步可分为几部分的内容:统一时钟源,硬件同步,软件同步。时间同步--时空一体--(时间-空间-物体)1.timespaceobject速度加速度角速度位置距离角度2.GPS卫星GPS基站GPS终端空间--太阳大气层地面空间干扰-环境遮蔽--空间隔......
  • logger & journalctl,LINUX日志管理
    简介:有时候写一些linux系统脚本,外加定时任务,总是希望能看到日志,所以就有了各种骚操作。一:自己写自己写入指定日志,并进行容量管理。#日志文件LOG_FILE=./log/log-$(date'+%Y-%m-%d').txtpath=./logcheck_logs(){if[!-d$path];thenmkdir$pathfi......
  • 2024-02-29-Linux高级网络编程(2-UDP编程)
    2.UDP编程2.1字节序概述字节序是指多字节数据的存储顺序分类:小端格式:将低位字节数据存储在低地址(LSB)大端格式:将高位字节数据存储在低地址2.1.1如何判断当前系统的字节序#include<stdio.h>unionun{/*data*/inta;charb;};intmain(intargc......
  • windows下Nginx启动失败(常见的两个错误以及解决方案)
    问题windows10下启动nginx,闪屏而过,访问localhost显示无法访问。尝试解决cmd下使用命令:netstat-an|find"0.0.0.0:80",可以发现80端口已经被占用。使用命令:netstat-ano可以发现占用80端口的服务pid=4,ctrl+shift+Esc打开任务管理器,查看详细,占先pid排序,可以查看到pid为4的......
  • linux查看ssh当前访问的ip地址
    netstat-anp|grep:22|grepESTABLISHED|awk'{print$5}'|cut-d:-f1|sort|uniq-c|sort-n参数解释:1.`netstat-anp`:列出所有的网络连接2.`grep:22`:筛选出SSH端口连接3.`grepESTABLISHED`:筛选出已经建立的连接4.`awk'{print$5}'`:提取出连接的IP地......
  • 故障分析 | linux 磁盘io利用率高,分析的正确姿势
    一、背景简介作为一个DBA难免不了会遇到性能问题,那么我们遇到性能问题该如何进行排查呢?例如我们在高并发的业务下,出现业务响应慢,处理时间长我们又该如何入手进行排查,本篇文章将分析io高的情况下如何分析及定位。二、环境复现环境配置:本次测试使用128C_512G_4TSSD服务器配置,MyS......
  • windows系统下 nssm 注册jar为windows服务并且守护进程
    1、下载nssmhttp://nssm.cc/download下载nssm2.24(2014-08-31)即可解压放在E:\nssm-2.242、新建启动jar脚本的bat文件D:\code2\1.bat内容是java-jarD:\code2\app.jar>log.log2>&1&\3、用nssm命令挂服务cdE:\nssm-2.24\win64nssminstallceshi"D:\code2\1.b......
  • Linux系统 - 使用 cmatrix 实现数字雨效果
    1.下载cmatrix源码包。可以在终端中使用wget命令从源码包提供的链接下载,命令如下:wget https://jaist.dl.sourceforge.net/project/cmatrix/cmatrix/1.2a/cmatrix-1.2a.tar.gz2.安装ncurses支持包。在终端中使用yum命令安装ncurses,命令如下:yuminstall-yncurses3.......