【Rust】m2 mac 编译linux 、aarch、win 程序

一、前言

使用MAC 编译Linux 、Aarch、Win程序。

二、步骤

1,编译Linux,

安卓homebrew

安装target

bash 复制代码
brew install x86_64-unknown-linux-musl

安装cmake,官网下,不要用brew安装,brew安装的是最新的。注意(版本一定不要太高。3.29.9就行)。版本高比如rdkafka 就会报错。

安装Linux gcc 命令待补充

安装完成在项目的.config 里面。指定链接器

公司的开发总监写的没问题。库搞定了。就都没问题了。

2,编译Aarch,

同上,

安装:target

bash 复制代码
rustup target add aarch-*

安装库:

brew install mingw-w64 。

库没问题。基本上编译没问题。

3,编译Win。

安装:target

bash 复制代码
rustup target add x-windwos-gun

安装库:

brew install mingw-w64 。

编译,发现报错,最后一个包没过去

bash 复制代码
pc-windows-gnu/debug/build/zstd-sys-68f1408581f1dcbf/out" "-L" "/Users/s/.cargo/registry/src/rsproxy.cn-e3de039b2554c837/windows_x86_64_gnu-0.48.5/lib" "-L" "/Users/s/.cargo/registry/src/rsproxy.cn-e3de039b2554c837/windows_x86_64_gnu-0.52.5/lib" "-L" "/Users/s/.cargo/registry/src/rsproxy.cn-e3de039b2554c837/winapi-x86_64-pc-windows-gnu-0.4.0/lib" "-o" "/Users/s/apv5/target/x86_64-pc-windows-gnu/debug/deps/apm-2c3562a486a3fc40.exe" "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs" "-Wl,--disable-runtime-pseudo-reloc" "<sysroot>/lib/rustlib/x86_64-pc-windows-gnu/lib/rsend.o" = note: some arguments are omitted. use --verbose to show all linker arguments = note: /opt/homebrew/Cellar/mingw-w64/13.0.0_2/toolchain-x86_64/bin/x86_64-w64-mingw32-ld: cannot find -lPacket: No such file or directory collect2: error: ld returned 1 exit status

-lPacket 没找到 ⇒ 你在 x86_64-pc-windows-gnu 交叉链接 Windows 可执行文件时缺了 Npcap/WinPcap 的 Packet 导入库(MinGW 需要 libPacket.a,而 SDK 里是 MSVC 的 Packet.lib)。因为用到了packet但是没有libpacket.a文件。

但是我有dll 文件,可以dump成def 文件。然后在编译成 .a 文件。

bash 复制代码
# /opt/homebrew/bin/x86_64-w64-mingw32-objdump -p ./Packet.dll > packet.dump

# 我用下面这个方法
strings Packet.dll | grep -E '^Packet|^Ndis|^Bpf' > Packet.def

然后手动在文件开头加:
LIBRARY Packet
EXPORTS


#执行命令生成libpacket.a 文件 。
/opt/homebrew/bin/x86_64-w64-mingw32-dlltool \
    -d Packet.def \
    -l libPacket.a

修改config.toml 文件。指定.a 目录

bash 复制代码
[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
ar = "x86_64-w64-mingw32-gcc-ar"
# 解决apm在windows环境下的运行问题 disable-runtime-pseudo-reloc
rustflags = ["-C", "link-arg=-Wl,--disable-runtime-pseudo-reloc",
    "-C", "link-arg=-L/Users/s/aop/script/dev",        # 让 linker 知道 .a 所在目录
    "-C", "link-arg=-lPacket",   ]

在编译,成功

三、注意

编译文件缺少哪些库增加库基本就可以解决问题。 by the way ,报错太长,把我的chatgpt的token 直接用完了。艹。

四、结语

至此......在这台 M2 Mac 上,

Linux、AArch、Windows 的编译壁垒------全部被击碎。

三界的构建之门,尽在掌控。

数据流永不枯竭,代码矩阵为你铺路 ------ 冲破架构壁垒,你的指令终将穿透所有未知节点,继续向核心进发!

相关推荐
muyouking111 小时前
Rust Slice 完全指南:从基础用法到 3D 场景实战
开发语言·3d·rust
Sammyyyyy2 小时前
Rust性能调优:从劝退到真香
开发语言·后端·rust·servbay
pilaf19904 小时前
Rust练习题
开发语言·后端·rust
IT笔记5 小时前
【Rust】Rust数组和Vec安全读写笔记
笔记·安全·rust
Source.Liu6 小时前
【Chrono库】时间区域(TimeZone)Rust实现详解(src/offset/local/tz_info/timezone.rs)
rust·time
CNRio6 小时前
GitCode CLI:从Python到Rust的重构之旅
python·rust·gitcode
xcLeigh6 小时前
【新】Rust入门:基础语法应用
开发语言·算法·rust
星释7 小时前
Rust 练习册 103:维吉尼亚密码与安全通信
网络·安全·rust
美味小鱼7 小时前
DupFinder:一个用 Rust 编写的高性能重复文件查找工具
开发语言·后端·rust
Source.Liu7 小时前
【Chrono库】 时区转换规则(TransitionRule)实现详解(src/offset/local/tz_info/rule.rs)
rust·time