【RUST 实战】交叉编译之Windows To Linux

简介: 【RUST 实战】交叉编译之Windows To Linux

0x00 开篇


什么是交叉编译?可能有的同学不理解。我这里简单介绍下,所谓交叉编译,就是在一个平台上可以编译生成另一个平台的可执行文件。这节课主要介绍如何在Windows系统上编译生成Linux系统的可执行文件。 


0x01 准备工作


我们跨平台编译,要准备如下一些前期工作。


操作环境


  • Windows 11
  • VS2012(VS2019我也测试过)
  • CLion
  • Rust 1.56.1
  • CentOS 7

添加Target


这里我所用的Linux是64位的,所以我这里需要添加x86_64-unknown-linux-musl 的这个target

rustup target add x86_64-unknown-linux-musl

通过下面的命令可以查看所有支持的target和已经安装的target。如果已安装会在后面显示(installed)

rustup target list


新建项目


新建一个普通项目cross_compile_test。这里我们以默认输出hello world的程序为例。


0a2653c851af460fa595bd959398a8f1.png


添加配置文件


配置文件可以针对某个项目,当然也可以在全局配置。下面以针对单个项目为例。

在项目目录下添加.cargo文件夹,在.cargo文件夹下添加config.toml文件。并且在config.toml中输入以下内容。

[target.x86_64-unknown-linux-musl]
linker = "rust-lld"
rustflags = ["-C", "linker-flavor=ld.lld"]

PS:关于全局配置,可以把上面的配置内容,复制到"C:/Users/当前用户名/.cargo/config"文件中,如果没有可以自行创建。config文件可以带toml扩展名,也可以不带。

最终结果如下图所示:


2d65d23f6d4748949b924e4057485923.png

 

0x02 项目编译


终端执行下面的命令,编译为Linux可执行二进制文件。如果不加--release则默认以debug模式编译。

cargo build --release --target=x86_64-unknown-linux-musl


6de278e6d6694ce5bb08e7e842b7e74b.png


编译完成后,会自动生成target文件夹,在target/x86_64-unknown-linux-musl/release文件夹下的cross_compile_test就是最终的Linux可执行二进制文件。 


0x03 文件测试


我们将文件拷贝到CentOS中测试。可以完美执行~~~


 8ec4f2997fb246878c34ecd6d122b7c6.png


0x04 扩展——Windows静态编译


不知道你是否测试过下面的这个场景。在Windows上,如果把每次使用cargo build命令编译后生成.exe可执行文件复制到其它电脑上运行,是无法运行的。这里呢,我们需要使用静态编译。否则,在其它Windows电脑上将会报错。


静态编译的方法也很简单,只需在配置文件中加入下面的代码。

[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]

然后再使用下面的命令编译即可。

cargo build --release --target=x86_64-pc-windows-msvc

具体过程同上面的Linux二进制文件编译过程。这里就不再详细叙述了。


相关文章
|
2月前
|
Web App开发 安全 Ubuntu
Nexpose 8.13.0 for Linux & Windows - 漏洞扫描
Nexpose 8.13.0 for Linux & Windows - 漏洞扫描
99 3
Nexpose 8.13.0 for Linux & Windows - 漏洞扫描
|
1月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025080401 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025080401 (Linux, Windows) - 专业渗透测试框架
64 0
|
30天前
|
安全 Linux iOS开发
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
184 0
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
|
30天前
|
安全 Linux 生物认证
Nexpose 8.18.0 for Linux & Windows - 漏洞扫描
Nexpose 8.18.0 for Linux & Windows - 漏洞扫描
44 0
Nexpose 8.18.0 for Linux & Windows - 漏洞扫描
|
1月前
|
安全 Linux iOS开发
SonarQube Server 2025 Release 4.2 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 4.2 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
108 0
SonarQube Server 2025 Release 4.2 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
1月前
|
NoSQL IDE MongoDB
Studio 3T 2025.14 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.14 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
117 0
Studio 3T 2025.14 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
2月前
|
Linux API iOS开发
Blender 4.5 (Linux, macOS, Windows) - 开源 3D 创意软件 (渲染 建模 雕刻)
Blender 4.5 (Linux, macOS, Windows) - 开源 3D 创意软件 (渲染 建模 雕刻)
120 1
Blender 4.5 (Linux, macOS, Windows) - 开源 3D 创意软件 (渲染 建模 雕刻)
|
2月前
|
安全 Linux iOS开发
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
308 0
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
2月前
|
Linux 虚拟化 iOS开发
VMware Workstation 17.6.4 Pro Unlocker & OEM BIOS 2.7 for Windows & Linux
VMware Workstation 17.6.4 Pro Unlocker & OEM BIOS 2.7 for Windows & Linux
597 0
VMware Workstation 17.6.4 Pro Unlocker & OEM BIOS 2.7 for Windows & Linux