尧图网络科技YAOTU DIGITAL 获取报价
获取报价
首页 / 资讯中心 / 文章详情

Rust跨平台开发

发布时间:2026/9/7 19:10:37

资讯中心
01
ARTICLE

Rust跨平台开发

Rust跨平台开发
‌Rust是一门系统编程语言专注于安全尤其是并发安全支持函数式、命令式和泛型等多范式编程‌。同时Rust也是跨平台语言。它支持在多种操作系统和硬件架构上编译运行开发者可以使用同一套代码生成不同平台的可执行文件 。‌‌‌支持平台广泛‌涵盖 Windows、Linux、macOS 等桌面系统以及 Android、iOS 移动端和各类嵌入式设备 。本篇记录编译生成在ARM架构的嵌入式设备上运行的Rust程序开发环境虚拟机Ubuntu20041.添加目前编译器# 查看已安装目标rustup target list --installed# 添加 ARM64 GNU 目标使用系统 glibcrustup target add aarch64-unknown-linux-gnu# 添加 ARM64 MUSL 目标静态链接rustup target add aarch64-unknown-linux-musl# 查看已安装的目标rustup target list --installedscottubuntu2004:~$ rustup update stable info: syncing channel updates for stable-x86_64-unknown-linux-gnu info: latest update on 2026-03-05, rust version 1.94.0 (4a4ef493e 2026-03-02) info: downloading component cargo 10.5 MiB / 10.5 MiB (100 %) 4.9 MiB/s in 2s info: downloading component clippy info: downloading component rust-docs 20.6 MiB / 20.6 MiB (100 %) 4.9 MiB/s in 4s info: downloading component rust-std 28.4 MiB / 28.4 MiB (100 %) 4.7 MiB/s in 6s info: downloading component rustc 74.9 MiB / 74.9 MiB (100 %) 4.5 MiB/s in 17s info: downloading component rustfmt info: removing previous version of component cargo info: removing previous version of component clippy info: removing previous version of component rust-docs info: removing previous version of component rust-std info: removing previous version of component rustc info: removing previous version of component rustfmt info: installing component cargo info: installing component clippy info: installing component rust-docs 20.6 MiB / 20.6 MiB (100 %) 11.2 MiB/s in 1s info: installing component rust-std 28.4 MiB / 28.4 MiB (100 %) 22.8 MiB/s in 1s info: installing component rustc 74.9 MiB / 74.9 MiB (100 %) 23.7 MiB/s in 3s info: installing component rustfmt stable-x86_64-unknown-linux-gnu updated - rustc 1.94.0 (4a4ef493e 2026-03-02) (from rustc 1.93.0 (254b59607 2026-01-19)) info: checking for self-update error: could not download file from https://mirrors.tuna.tsinghua.edu.cn/rustup/release-stable.toml to /tmp/rustup-updatez6MtR0/release-stable.toml: http request returned an unsuccessful status code: 404scottubuntu2004:~$ rustc --version rustc 1.94.0 (4a4ef493e 2026-03-02) scottubuntu2004:~$ rustup target add aarch64-unknown-linux-musl info: downloading component rust-std for aarch64-unknown-linux-musl info: installing component rust-std for aarch64-unknown-linux-musl 26.0 MiB / 26.0 MiB (100 %) 22.8 MiB/s in 1s scottubuntu2004:~$ rustup target add aarch64-unknown-linux-gnu info: downloading component rust-std for aarch64-unknown-linux-gnu info: installing component rust-std for aarch64-unknown-linux-gnu 27.2 MiB / 27.2 MiB (100 %) 23.1 MiB/s in 1s scottubuntu2004:~$ rustup target list --installed aarch64-unknown-linux-gnu aarch64-unknown-linux-musl x86_64-unknown-linux-gnu scottubuntu2004:~$2.设置全局跨平台编译配置文件新建配置文件.cargo/config.toml/home/scott/.cargo/config.toml内容如下# ~/.cargo/config.toml [target.aarch64-unknown-linux-gnu] linker aarch64-linux-gnu-gcc # 如果需要其他库添加搜索路径 # rustflags [-L, /usr/aarch64-linux-gnu/lib] [target.aarch64-unknown-linux-musl] linker aarch64-linux-musl-gcc3.编译代码测试cargo new hello创建项目创建成功后hello目录下有Cargo.toml文件和src目录src目录里有main.rs文件编写代码fn main() { println!(This is a Rust project in the Ubuntu environment!); println!(Hello, world!); }保存退出然后执行cargo build命令进行编译编译成功后执行cargo run命令运行。scottubuntu2004:~/trunk/rustbyexample/chapter01$ cargo new hello Creating binary (application) hello package note: see more Cargo.toml keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html scottubuntu2004:~/trunk/rustbyexample/chapter01$ cd hello scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ ls Cargo.toml src scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ cd src scottubuntu2004:~/trunk/rustbyexample/chapter01/hello/src$ ls main.rs scottubuntu2004:~/trunk/rustbyexample/chapter01/hello/src$ vim main.rs scottubuntu2004:~/trunk/rustbyexample/chapter01/hello/src$ cd .. scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ cargo build Compiling hello v0.1.0 (/home/scott/trunk/rustbyexample/chapter01/hello) Finished dev profile [unoptimized debuginfo] target(s) in 0.54s scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ cargo run Finished dev profile [unoptimized debuginfo] target(s) in 0.00s Running target/debug/hello This is a Rust project in the Ubuntu environment! Hello, world! scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$项目结构hello/ ├── Cargo.toml └── src/ └── main.rsUbuntu平台编译成功下面进行跨平台编译首先编译aarch64-unknown-linux-gnu。cargo build --targetaarch64-unknown-linux-gnu --releasescottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ cargo build --targetaarch64-unknown-linux-gnu --release Compiling hello v0.1.0 (/home/scott/trunk/rustbyexample/chapter01/hello) error: linking with cc failed: exit status: 1 | note: cc /home/scott/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu/release/deps/rustcIUGtLr/symbols.o 2 object files omitted -Wl,--as-needed -Wl,-Bstatic sysroot/lib/rustlib/aarch64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,libcfg_if-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib -Wl,-Bdynamic -lgcc_s -lutil -lrt -lpthread -lm -ldl -lc -L /home/scott/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu/release/deps/rustcIUGtLr/raw-dylibs -Bsysroot/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld -fuse-ldlld -Wl,--eh-frame-hdr -Wl,-z,noexecstack -L sysroot/lib/rustlib/aarch64-unknown-linux-gnu/lib -o /home/scott/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu/release/deps/hello-45a8574ff1ff12aa -Wl,--gc-sections -pie -Wl,-z,relro,-z,now -Wl,-O1 -Wl,--strip-debug -nodefaultlibs note: some arguments are omitted. use --verbose to show all linker arguments note: rust-lld: error: /home/scott/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu/release/deps/rustcIUGtLr/symbols.o is incompatible with elf64-x86-64 rust-lld: error: /home/scott/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu/release/deps/hello-45a8574ff1ff12aa.hello.a7b0cd2e8628662d-cgu.0.rcgu.o is incompatible with elf64-x86-64 rust-lld: error: /home/scott/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu/release/deps/hello-45a8574ff1ff12aa.6kwgw2jxqbicf638la2oftp62.rcgu.o is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libstd-e55595babf65b9b2.rlib(lib.rmeta) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libstd-e55595babf65b9b2.rlib(std-e55595babf65b9b2.std.2c034892e08a5f18-cgu.0.rcgu.o) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libpanic_unwind-308523226fff4ba5.rlib(lib.rmeta) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libpanic_unwind-308523226fff4ba5.rlib(panic_unwind-308523226fff4ba5.panic_unwind.56b1f1d6a2efeb15-cgu.0.rcgu.o) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libobject-c2a0a9ba3ca8250c.rlib(lib.rmeta) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libobject-c2a0a9ba3ca8250c.rlib(object-c2a0a9ba3ca8250c.object.bc439bcd3b626dc-cgu.0.rcgu.o) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libmemchr-3d2c9a2ce7dac46a.rlib(lib.rmeta) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libmemchr-3d2c9a2ce7dac46a.rlib(memchr-3d2c9a2ce7dac46a.memchr.d59623ef6b85ef39-cgu.0.rcgu.o) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libaddr2line-82482b918109940e.rlib(lib.rmeta) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libaddr2line-82482b918109940e.rlib(addr2line-82482b918109940e.addr2line.f8f46de18d7bbba2-cgu.0.rcgu.o) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libgimli-15dc331004570f15.rlib(lib.rmeta) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libgimli-15dc331004570f15.rlib(gimli-15dc331004570f15.gimli.3409884ab03f458a-cgu.0.rcgu.o) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libcfg_if-34d3c41c3a97d452.rlib(lib.rmeta) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libcfg_if-34d3c41c3a97d452.rlib(cfg_if-34d3c41c3a97d452.cfg_if.13b0b99b7a92f5d9-cgu.0.rcgu.o) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/librustc_demangle-d484d87d3c33bf9a.rlib(lib.rmeta) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/librustc_demangle-d484d87d3c33bf9a.rlib(rustc_demangle-d484d87d3c33bf9a.rustc_demangle.a4abe7ad73c7ed59-cgu.0.rcgu.o) is incompatible with elf64-x86-64 rust-lld: error: /home/scott/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libstd_detect-7d4a1d03b031cca3.rlib(lib.rmeta) is incompatible with elf64-x86-64 rust-lld: error: too many errors emitted, stopping now (use --error-limit0 to see all errors) collect2: error: ld returned 1 exit status error: could not compile hello (bin hello) due to 1 previous error scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$编译出错原因是还要安装交叉编译工具链。4.安装交叉编译工具链# 安装 GNU 工具链sudo apt-get updatesudo apt-get install -y \gcc-aarch64-linux-gnu \binutils-aarch64-linux-gnu \libc6-dev-arm64-crossscottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ sudo apt-get install -y gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu libc6-dev-arm64-cross [sudo] password for scott: Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: cpp-9-aarch64-linux-gnu cpp-aarch64-linux-gnu gcc-10-cross-base gcc-9-aarch64-linux-gnu gcc-9-aarch64-linux-gnu-base gcc-9-cross-base libasan5-arm64-cross libatomic1-arm64-cross libc6-arm64-cross libgcc-9-dev-arm64-cross libgcc-s1-arm64-cross libgomp1-arm64-cross libitm1-arm64-cross liblsan0-arm64-cross libstdc6-arm64-cross libtsan0-arm64-cross libubsan1-arm64-cross linux-libc-dev-arm64-cross Suggested packages: binutils-doc gcc-9-locales cpp-doc gcc-9-doc autoconf automake libtool flex bison gdb-aarch64-linux-gnu gcc-doc The following NEW packages will be installed: binutils-aarch64-linux-gnu cpp-9-aarch64-linux-gnu cpp-aarch64-linux-gnu gcc-10-cross-base gcc-9-aarch64-linux-gnu gcc-9-aarch64-linux-gnu-base gcc-9-cross-base gcc-aarch64-linux-gnu libasan5-arm64-cross libatomic1-arm64-cross libc6-arm64-cross libc6-dev-arm64-cross libgcc-9-dev-arm64-cross libgcc-s1-arm64-cross libgomp1-arm64-cross libitm1-arm64-cross liblsan0-arm64-cross libstdc6-arm64-cross libtsan0-arm64-cross libubsan1-arm64-cross linux-libc-dev-arm64-cross 0 upgraded, 21 newly installed, 0 to remove and 70 not upgraded. Need to get 28.8 MB of archives. After this operation, 114 MB of additional disk space will be used. Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 gcc-9-aarch64-linux-gnu-base amd64 9.4.0-1ubuntu1~20.04.2cross2 [19.1 kB] Get:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 cpp-9-aarch64-linux-gnu amd64 9.4.0-1ubuntu1~20.04.2cross2 [6,677 kB] Get:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 cpp-aarch64-linux-gnu amd64 4:9.3.0-1ubuntu2 [3,432 B] Get:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 gcc-10-cross-base all 10.5.0-1ubuntu1~20.04cross1 [15.5 kB] Get:5 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 binutils-aarch64-linux-gnu amd64 2.34-6ubuntu1.11 [2,773 kB] Get:6 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 gcc-9-cross-base all 9.4.0-1ubuntu1~20.04.2cross2 [14.4 kB] Get:7 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libc6-arm64-cross all 2.31-0ubuntu9.9cross1 [1,050 kB] Get:8 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libgcc-s1-arm64-cross all 10.5.0-1ubuntu1~20.04cross1 [34.6 kB] Get:9 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libgomp1-arm64-cross all 10.5.0-1ubuntu1~20.04cross1 [91.4 kB] Get:10 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libitm1-arm64-cross all 10.5.0-1ubuntu1~20.04cross1 [23.6 kB] Get:11 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libatomic1-arm64-cross all 10.5.0-1ubuntu1~20.04cross1 [9,520 B] Get:12 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libasan5-arm64-cross all 9.4.0-1ubuntu1~20.04.2cross2 [2,678 kB] Get:13 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 liblsan0-arm64-cross all 10.5.0-1ubuntu1~20.04cross1 [801 kB] Get:14 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libtsan0-arm64-cross all 10.5.0-1ubuntu1~20.04cross1 [1,964 kB] Get:15 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libstdc6-arm64-cross all 10.5.0-1ubuntu1~20.04cross1 [420 kB] Get:16 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libubsan1-arm64-cross all 10.5.0-1ubuntu1~20.04cross1 [765 kB] Get:17 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libgcc-9-dev-arm64-cross all 9.4.0-1ubuntu1~20.04.2cross2 [919 kB] Get:18 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 gcc-9-aarch64-linux-gnu amd64 9.4.0-1ubuntu1~20.04.2cross2 [7,428 kB] Get:19 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 gcc-aarch64-linux-gnu amd64 4:9.3.0-1ubuntu2 [1,420 B] Get:20 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 linux-libc-dev-arm64-cross all 5.4.0-110.124cross1 [1,052 kB] Get:21 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 libc6-dev-arm64-cross all 2.31-0ubuntu9.9cross1 [2,053 kB] Fetched 28.8 MB in 7s (4,412 kB/s) Selecting previously unselected package gcc-9-aarch64-linux-gnu-base:amd64. (Reading database ... 194113 files and directories currently installed.) Preparing to unpack .../00-gcc-9-aarch64-linux-gnu-base_9.4.0-1ubuntu1~20.04.2cross2_amd64.deb ... Unpacking gcc-9-aarch64-linux-gnu-base:amd64 (9.4.0-1ubuntu1~20.04.2cross2) ... Selecting previously unselected package cpp-9-aarch64-linux-gnu. Preparing to unpack .../01-cpp-9-aarch64-linux-gnu_9.4.0-1ubuntu1~20.04.2cross2_amd64.deb ... Unpacking cpp-9-aarch64-linux-gnu (9.4.0-1ubuntu1~20.04.2cross2) ... Selecting previously unselected package cpp-aarch64-linux-gnu. Preparing to unpack .../02-cpp-aarch64-linux-gnu_4%3a9.3.0-1ubuntu2_amd64.deb ... Unpacking cpp-aarch64-linux-gnu (4:9.3.0-1ubuntu2) ... Selecting previously unselected package gcc-10-cross-base. Preparing to unpack .../03-gcc-10-cross-base_10.5.0-1ubuntu1~20.04cross1_all.deb ... Unpacking gcc-10-cross-base (10.5.0-1ubuntu1~20.04cross1) ... Selecting previously unselected package binutils-aarch64-linux-gnu. Preparing to unpack .../04-binutils-aarch64-linux-gnu_2.34-6ubuntu1.11_amd64.deb ... Unpacking binutils-aarch64-linux-gnu (2.34-6ubuntu1.11) ... Selecting previously unselected package gcc-9-cross-base. Preparing to unpack .../05-gcc-9-cross-base_9.4.0-1ubuntu1~20.04.2cross2_all.deb ... Unpacking gcc-9-cross-base (9.4.0-1ubuntu1~20.04.2cross2) ... Selecting previously unselected package libc6-arm64-cross. Preparing to unpack .../06-libc6-arm64-cross_2.31-0ubuntu9.9cross1_all.deb ... Unpacking libc6-arm64-cross (2.31-0ubuntu9.9cross1) ... Selecting previously unselected package libgcc-s1-arm64-cross. Preparing to unpack .../07-libgcc-s1-arm64-cross_10.5.0-1ubuntu1~20.04cross1_all.deb ... Unpacking libgcc-s1-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Selecting previously unselected package libgomp1-arm64-cross. Preparing to unpack .../08-libgomp1-arm64-cross_10.5.0-1ubuntu1~20.04cross1_all.deb ... Unpacking libgomp1-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Selecting previously unselected package libitm1-arm64-cross. Preparing to unpack .../09-libitm1-arm64-cross_10.5.0-1ubuntu1~20.04cross1_all.deb ... Unpacking libitm1-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Selecting previously unselected package libatomic1-arm64-cross. Preparing to unpack .../10-libatomic1-arm64-cross_10.5.0-1ubuntu1~20.04cross1_all.deb ... Unpacking libatomic1-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Selecting previously unselected package libasan5-arm64-cross. Preparing to unpack .../11-libasan5-arm64-cross_9.4.0-1ubuntu1~20.04.2cross2_all.deb ... Unpacking libasan5-arm64-cross (9.4.0-1ubuntu1~20.04.2cross2) ... Selecting previously unselected package liblsan0-arm64-cross. Preparing to unpack .../12-liblsan0-arm64-cross_10.5.0-1ubuntu1~20.04cross1_all.deb ... Unpacking liblsan0-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Selecting previously unselected package libtsan0-arm64-cross. Preparing to unpack .../13-libtsan0-arm64-cross_10.5.0-1ubuntu1~20.04cross1_all.deb ... Unpacking libtsan0-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Selecting previously unselected package libstdc6-arm64-cross. Preparing to unpack .../14-libstdc6-arm64-cross_10.5.0-1ubuntu1~20.04cross1_all.deb ... Unpacking libstdc6-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Selecting previously unselected package libubsan1-arm64-cross. Preparing to unpack .../15-libubsan1-arm64-cross_10.5.0-1ubuntu1~20.04cross1_all.deb ... Unpacking libubsan1-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Selecting previously unselected package libgcc-9-dev-arm64-cross. Preparing to unpack .../16-libgcc-9-dev-arm64-cross_9.4.0-1ubuntu1~20.04.2cross2_all.deb ... Unpacking libgcc-9-dev-arm64-cross (9.4.0-1ubuntu1~20.04.2cross2) ... Selecting previously unselected package gcc-9-aarch64-linux-gnu. Preparing to unpack .../17-gcc-9-aarch64-linux-gnu_9.4.0-1ubuntu1~20.04.2cross2_amd64.deb ... Unpacking gcc-9-aarch64-linux-gnu (9.4.0-1ubuntu1~20.04.2cross2) ... Selecting previously unselected package gcc-aarch64-linux-gnu. Preparing to unpack .../18-gcc-aarch64-linux-gnu_4%3a9.3.0-1ubuntu2_amd64.deb ... Unpacking gcc-aarch64-linux-gnu (4:9.3.0-1ubuntu2) ... Selecting previously unselected package linux-libc-dev-arm64-cross. Preparing to unpack .../19-linux-libc-dev-arm64-cross_5.4.0-110.124cross1_all.deb ... Unpacking linux-libc-dev-arm64-cross (5.4.0-110.124cross1) ... Selecting previously unselected package libc6-dev-arm64-cross. Preparing to unpack .../20-libc6-dev-arm64-cross_2.31-0ubuntu9.9cross1_all.deb ... Unpacking libc6-dev-arm64-cross (2.31-0ubuntu9.9cross1) ... Setting up gcc-9-aarch64-linux-gnu-base:amd64 (9.4.0-1ubuntu1~20.04.2cross2) ... Setting up binutils-aarch64-linux-gnu (2.34-6ubuntu1.11) ... Setting up cpp-9-aarch64-linux-gnu (9.4.0-1ubuntu1~20.04.2cross2) ... Setting up cpp-aarch64-linux-gnu (4:9.3.0-1ubuntu2) ... Setting up libc6-arm64-cross (2.31-0ubuntu9.9cross1) ... Setting up gcc-9-cross-base (9.4.0-1ubuntu1~20.04.2cross2) ... Setting up gcc-10-cross-base (10.5.0-1ubuntu1~20.04cross1) ... Setting up linux-libc-dev-arm64-cross (5.4.0-110.124cross1) ... Setting up libgcc-s1-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Setting up libatomic1-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Setting up liblsan0-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Setting up libgomp1-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Setting up libtsan0-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Setting up libc6-dev-arm64-cross (2.31-0ubuntu9.9cross1) ... Setting up libasan5-arm64-cross (9.4.0-1ubuntu1~20.04.2cross2) ... Setting up libstdc6-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Setting up libitm1-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Setting up libubsan1-arm64-cross (10.5.0-1ubuntu1~20.04cross1) ... Setting up libgcc-9-dev-arm64-cross (9.4.0-1ubuntu1~20.04.2cross2) ... Setting up gcc-9-aarch64-linux-gnu (9.4.0-1ubuntu1~20.04.2cross2) ... Setting up gcc-aarch64-linux-gnu (4:9.3.0-1ubuntu2) ... Processing triggers for man-db (2.9.1-1) ... Processing triggers for libc-bin (2.31-0ubuntu9.18) ... scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ aarch64-linux-gnu-gcc --version aarch64-linux-gnu-gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$# 安装 MUSL 工具链用于静态编译sudo apt-get install -y musl-toolsscottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ sudo apt-get install -y musl-tools Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: musl musl-dev Recommended packages: linux-musl-dev The following NEW packages will be installed: musl musl-dev musl-tools 0 upgraded, 3 newly installed, 0 to remove and 70 not upgraded. Need to get 948 kB of archives. After this operation, 4,367 kB of additional disk space will be used. Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/universe amd64 musl amd64 1.1.24-1 [377 kB] Get:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/universe amd64 musl-dev amd64 1.1.24-1 [565 kB] Get:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/universe amd64 musl-tools amd64 1.1.24-1 [5,868 B] Fetched 948 kB in 1s (935 kB/s) Selecting previously unselected package musl:amd64. (Reading database ... 196178 files and directories currently installed.) Preparing to unpack .../musl_1.1.24-1_amd64.deb ... Unpacking musl:amd64 (1.1.24-1) ... Selecting previously unselected package musl-dev:amd64. Preparing to unpack .../musl-dev_1.1.24-1_amd64.deb ... Unpacking musl-dev:amd64 (1.1.24-1) ... Selecting previously unselected package musl-tools. Preparing to unpack .../musl-tools_1.1.24-1_amd64.deb ... Unpacking musl-tools (1.1.24-1) ... Setting up musl:amd64 (1.1.24-1) ... Setting up musl-dev:amd64 (1.1.24-1) ... Setting up musl-tools (1.1.24-1) ... Processing triggers for man-db (2.9.1-1) ... scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$安装成功再次编译scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ cargo build --targetaarch64-unknown-linux-gnu --release Compiling hello v0.1.0 (/home/scott/trunk/rustbyexample/chapter01/hello) Finished release profile [optimized] target(s) in 0.29s scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ cd target/ scottubuntu2004:~/trunk/rustbyexample/chapter01/hello/target$ ls aarch64-unknown-linux-gnu CACHEDIR.TAG debug release scottubuntu2004:~/trunk/rustbyexample/chapter01/hello/target$ cd aarch64-unknown-linux-gnu/ scottubuntu2004:~/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu$ ls CACHEDIR.TAG release scottubuntu2004:~/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu$ cd release/ scottubuntu2004:~/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu/release$ ls build deps examples hello hello.d incremental scottubuntu2004:~/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu/release$ file hello hello: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]8d56ab7e162f6f6b12b65fcd3f57732b58fd2795, for GNU/Linux 3.7.0, not stripped scottubuntu2004:~/trunk/rustbyexample/chapter01/hello/target/aarch64-unknown-linux-gnu/release$静态编译不依赖ARM平台的系统库 target.aarch64-unknown-linux-muslcargo build --targetaarch64-unknown-linux-musl --releasescottubuntu2004:~/trunk/rustbyexample/chapter01/hello$ cargo build --targetaarch64-unknown-linux-musl --release Compiling hello v0.1.0 (/home/scott/trunk/rustbyexample/chapter01/hello) error: linker aarch64-linux-musl-gcc not found | note: No such file or directory (os error 2) error: could not compile hello (bin hello) due to 1 previous error scottubuntu2004:~/trunk/rustbyexample/chapter01/hello$编译失败了虽然前面都安装成功了但找不到工具链。手动安装aarch64-linux-musl-gcc。从musl.cc下载预编译的工具链最可靠wget https://musl.cc/aarch64-linux-musl-cross.tgz sudo tar -xzf aarch64-linux-musl-cross.tgz -C /opt export PATH/opt/aarch64-linux-musl-cross/bin:$PATHcottubuntu2004:~/.cargo$ wget https://musl.cc/aarch64-linux-musl-cross.tgz --2026-03-10 10:44:17-- https://musl.cc/aarch64-linux-musl-cross.tgz Resolving musl.cc (musl.cc)... 216.82.192.11, 2602:fcdb:0:2::1:11 Connecting to musl.cc (musl.cc)|216.82.192.11|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 108096828 (103M) [application/octet-stream] Saving to: ‘aarch64-linux-musl-cross.tgz’ aarch64-linux-musl- 100%[] 103.09M 81.0KB/s in 35m 34s 2026-03-10 11:19:53 (49.5 KB/s) - ‘aarch64-linux-musl-cross.tgz’ saved [108096828/108096828] scottubuntu2004:~/.cargo$ sudo tar -xzf aarch64-linux-musl-cross.tgz -C /opt [sudo] password for scott: scottubuntu2004:~/.cargo$然后将export命令添加到你的~/.bashrc中永久生效。验证安装aarch64-linux-musl-gcc --version确保.cargo/config.toml中配置正确前面已配置toml# ~/.cargo/config.toml [target.aarch64-unknown-linux-gnu] linker aarch64-linux-gnu-gcc [target.aarch64-unknown-linux-musl] linker aarch64-linux-musl-gcc编译cargo build --targetaarch64-unknown-linux-musl --release查看工具链安装路径scottubuntu2004:~/.cargo$ whereis aarch64-linux-gnu-gcc aarch64-linux-gnu-gcc: /usr/bin/aarch64-linux-gnu-gcc /usr/share/man/man1/aarch64-linux-gnu-gcc.1.gz scottubuntu2004:~/.cargo$ whereis aarch64-linux-musl-gcc aarch64-linux-musl-gcc: /opt/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc scottubuntu2004:~/.cargo$# 编译你的项目 cargo build --targetaarch64-unknown-linux-gnu --release cargo build --targetaarch64-unknown-linux-musl --release # 这个二进制可以在任何 ARM64 Linux 上运行 #把hello复制到win共享目录下然后用scp命令拷贝到arm上 PS E:\ubuntuVBox\ubuntuShare scp ./hello root192.168.1.1:/usr/local总结中途装了很多其他的交叉编译工具都失败了比如cargo-zigbuildcross放到ARM版本测试时aarch64-unknown-linux-gnu版本生的程序运行不了aarch64-unknown-linux-musl静态编译的版本可以运行看来用静态编译的比较方便。
02
RELATED NEWS

相关资讯

更多网站建设与数字化升级内容

03
WHY YAOTU

想打造同款高转化官网?

懂行业、懂生意,从建站到增长一站式陪跑

场景化定制

不做模板站,围绕你的业务场景量身设计,小众不撞款。

营销型架构

以转化目标组织内容与路径,让官网真正带来询盘。

全周期服务

设计、开发、运营、运维一体,上线只是开始。

免费获取你的建站方案

留下需求,专属顾问 24 小时内为你输出方案建议。