我想交叉编译一个使用 tokio-udev 的最小项目。由于缺少 libudev,链接器失败:
aarch64-linux-musl/bin/ld: cannot find -ludev
我可以交叉编译不使用udev的Rust项目。 Host是运行Ubuntu的虚拟机x84_64,target是arm64。
项目的 Cargo.toml:
[package]
name = "myudev"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio-udev = "0.9.1"
和 src/main.rs:
use tokio_udev;
fn main() {
println!("Hello world");
let mut enumerator = tokio_udev::Enumerator::new().unwrap();
enumerator.match_subsystem("usb").unwrap();
for device in enumerator.scan_devices().unwrap() {
if let Some(sysname) = device.sysname().to_str() {
println!("Sysname: {:?}", sysname);
}
}
}
针对 x86_64 编译良好,运行良好(显然,有点难以判断,因为虚拟机没有 USB 设备,但打印了 hello 消息)。
不幸的是,docker镜像
ghcr.io/cross-rs/aarch64-unknown-linux-musl
我通常用于交叉编译
没有libudev,所以我创建了一个Dockerfile
FROM ghcr.io/cross-rs/aarch64-unknown-linux-musl
RUN apt-get update && \
apt-get install -y less && \
apt-get install -y libudev-dev libusb-1.0-0-dev
RUN dpkg --add-architecture arm64 && \
apt-get update && \
apt-get install -y libudev-dev:arm64 libusb-1.0-0-dev:arm64
使用
docker build -t cross-aarch64-unknown-linux-musl-udev .
构建一个新的 docker 镜像,效果很好。
我创建了一个 Cross.toml:
[target.aarch64-unknown-linux-musl]
image = "cross-aarch64-unknown-linux-musl-udev"
这根本没有帮助(同样的 ld 错误)。
这里,猜测开始。
libudev-dev:amd64
(dpkg --get-selections|grep libudev
表明为 arm64 和 amd64 安装了 libudev-dev 和 libudev1)。/lib/aarch64-linux-gnu/libudev.so.1.6.9
,它是 ARM aarch64 共享库。/usr/lib/x86_64-linux-gnu/pkgconfig/libudev.pc
和/usr/lib/aarch64-linux-gnu/pkgconfig/libudev.pc
提到libdir=/lib/
*ENV PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig
ENV PKG_CONFIG_DIR=${PKG_CONFIG_PATH}
ENV PKG_CONFIG_ALLOW_CROSS=1
但我还是明白
$ cross run --target aarch64-unknown-linux-musl
Compiling libudev-sys v0.1.4
Compiling udev v0.7.0
Compiling tokio-udev v0.9.1
Compiling myudev v0.1.0 (/project)
error: linking with `aarch64-linux-musl-gcc.sh` failed: exit status: 1
|
= note: LC_ALL="C" PATH="/rust/lib/rustlib/x86_64-unknown-linux-gnu/bin:/rust/lib/rustlib/x86_64-unknown-linux-gnu/bin/self-contained:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/rust/bin" VSLANG="1033" "aarch64-linux-musl-gcc.sh" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crt1.o" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crti.o" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtbegin.o" "/tmp/rustcN0HUq8/symbols.o" "/target/aarch64-unknown-linux-musl/release/deps/myudev-4276b24e2eda4c6f.myudev.e2e18e861efab55f-cgu.0.rcgu.o" "/target/aarch64-unknown-linux-musl/release/deps/myudev-4276b24e2eda4c6f.4un2eju0wjg1e8h1.rcgu.o" "-Wl,--as-needed" "-L" "/target/aarch64-unknown-linux-musl/release/deps" "-L" "/target/release/deps" "-L" "/lib/aarch64-linux-gnu" "-L" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib" "-Wl,-Bstatic" "/target/aarch64-unknown-linux-musl/release/deps/libtokio_udev-7798a6a06b8b7b80.rlib" "/target/aarch64-unknown-linux-musl/release/deps/libtokio-5bbcd21ab88f1035.rlib" "/target/aarch64-unknown-linux-musl/release/deps/libsocket2-d6668f2ac4e642ea.rlib" "/target/aarch64-unknown-linux-musl/release/deps/libpin_project_lite-d2cfa549eebd2c5f.rlib" "/target/aarch64-unknown-linux-musl/release/deps/libmio-e1eda35321fb180a.rlib" "/target/aarch64-unknown-linux-musl/release/deps/libfutures_core-68de2db109c4f33d.rlib" "/target/aarch64-unknown-linux-musl/release/deps/libudev-c5959f99f01f0940.rlib" "/target/aarch64-unknown-linux-musl/release/deps/liblibudev_sys-ef0039dcf2d0868b.rlib" "-ludev" "/target/aarch64-unknown-linux-musl/release/deps/liblibc-2f38b5feca90ac8d.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libstd-3aedbc8e8c0e1607.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libpanic_unwind-43c077dbaac5c651.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libobject-4cacc5fa8a27ecf8.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libmemchr-64f6783c29437be0.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libaddr2line-686d787984217b59.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libgimli-514530b69e03e0ef.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_demangle-6d8857037a7ba1ac.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libstd_detect-87900d26b9dcad12.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libhashbrown-e7a3685c0bbd0c42.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_std_workspace_alloc-38ecd594301af8ea.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libminiz_oxide-1ab977cb49e9f850.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libadler-193dd2a79ef160a9.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libunwind-8dea20a203d09b19.rlib" "-lunwind" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libcfg_if-b1bae776a6e5677d.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-d09271687f61e1fb.rlib" "-lc" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/liballoc-d02027f3bdb9876a.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_std_workspace_core-d87b0cd085418983.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libcore-8135530b14978e6d.rlib" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-08c46ed993ccb312.rlib" "-Wl,-Bdynamic" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-nostartfiles" "-L" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib" "-L" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained" "-o" "/target/aarch64-unknown-linux-musl/release/deps/myudev-4276b24e2eda4c6f" "-Wl,--gc-sections" "-static" "-no-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-nodefaultlibs" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtend.o" "/rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtn.o"
= note: + set -euo pipefail
+ main /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crt1.o /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crti.o /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtbegin.o /tmp/rustcN0HUq8/symbols.o /target/aarch64-unknown-linux-musl/release/deps/myudev-4276b24e2eda4c6f.myudev.e2e18e861efab55f-cgu.0.rcgu.o /target/aarch64-unknown-linux-musl/release/deps/myudev-4276b24e2eda4c6f.4un2eju0wjg1e8h1.rcgu.o -Wl,--as-needed -L /target/aarch64-unknown-linux-musl/release/deps -L /target/release/deps -L /lib/aarch64-linux-gnu -L /rust/lib/rustlib/aarch64-unknown-linux-musl/lib -Wl,-Bstatic /target/aarch64-unknown-linux-musl/release/deps/libtokio_udev-7798a6a06b8b7b80.rlib /target/aarch64-unknown-linux-musl/release/deps/libtokio-5bbcd21ab88f1035.rlib /target/aarch64-unknown-linux-musl/release/deps/libsocket2-d6668f2ac4e642ea.rlib /target/aarch64-unknown-linux-musl/release/deps/libpin_project_lite-d2cfa549eebd2c5f.rlib /target/aarch64-unknown-linux-musl/release/deps/libmio-e1eda35321fb180a.rlib /target/aarch64-unknown-linux-musl/release/deps/libfutures_core-68de2db109c4f33d.rlib /target/aarch64-unknown-linux-musl/release/deps/libudev-c5959f99f01f0940.rlib /target/aarch64-unknown-linux-musl/release/deps/liblibudev_sys-ef0039dcf2d0868b.rlib -ludev /target/aarch64-unknown-linux-musl/release/deps/liblibc-2f38b5feca90ac8d.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libstd-3aedbc8e8c0e1607.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libpanic_unwind-43c077dbaac5c651.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libobject-4cacc5fa8a27ecf8.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libmemchr-64f6783c29437be0.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libaddr2line-686d787984217b59.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libgimli-514530b69e03e0ef.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_demangle-6d8857037a7ba1ac.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libstd_detect-87900d26b9dcad12.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libhashbrown-e7a3685c0bbd0c42.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_std_workspace_alloc-38ecd594301af8ea.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libminiz_oxide-1ab977cb49e9f850.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libadler-193dd2a79ef160a9.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libunwind-8dea20a203d09b19.rlib -lunwind /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libcfg_if-b1bae776a6e5677d.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-d09271687f61e1fb.rlib -lc /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/liballoc-d02027f3bdb9876a.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_std_workspace_core-d87b0cd085418983.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libcore-8135530b14978e6d.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-08c46ed993ccb312.rlib -Wl,-Bdynamic -Wl,--eh-frame-hdr -Wl,-z,noexecstack -nostartfiles -L /rust/lib/rustlib/aarch64-unknown-linux-musl/lib -L /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained -o /target/aarch64-unknown-linux-musl/release/deps/myudev-4276b24e2eda4c6f -Wl,--gc-sections -static -no-pie -Wl,-z,relro,-z,now -Wl,-O1 -nodefaultlibs /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtend.o /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtn.o
+ local release=
++ rustc -Vv
++ cut -d : -f2
++ grep '^release:'
+ release=' 1.78.0'
+ local minor=
++ echo ' 1.78.0'
++ cut -d . -f2
+ minor=78
+ (( minor >= 48 ))
+ aarch64-linux-musl-gcc /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crt1.o /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crti.o /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtbegin.o /tmp/rustcN0HUq8/symbols.o /target/aarch64-unknown-linux-musl/release/deps/myudev-4276b24e2eda4c6f.myudev.e2e18e861efab55f-cgu.0.rcgu.o /target/aarch64-unknown-linux-musl/release/deps/myudev-4276b24e2eda4c6f.4un2eju0wjg1e8h1.rcgu.o -Wl,--as-needed -L /target/aarch64-unknown-linux-musl/release/deps -L /target/release/deps -L /lib/aarch64-linux-gnu -L /rust/lib/rustlib/aarch64-unknown-linux-musl/lib -Wl,-Bstatic /target/aarch64-unknown-linux-musl/release/deps/libtokio_udev-7798a6a06b8b7b80.rlib /target/aarch64-unknown-linux-musl/release/deps/libtokio-5bbcd21ab88f1035.rlib /target/aarch64-unknown-linux-musl/release/deps/libsocket2-d6668f2ac4e642ea.rlib /target/aarch64-unknown-linux-musl/release/deps/libpin_project_lite-d2cfa549eebd2c5f.rlib /target/aarch64-unknown-linux-musl/release/deps/libmio-e1eda35321fb180a.rlib /target/aarch64-unknown-linux-musl/release/deps/libfutures_core-68de2db109c4f33d.rlib /target/aarch64-unknown-linux-musl/release/deps/libudev-c5959f99f01f0940.rlib /target/aarch64-unknown-linux-musl/release/deps/liblibudev_sys-ef0039dcf2d0868b.rlib -ludev /target/aarch64-unknown-linux-musl/release/deps/liblibc-2f38b5feca90ac8d.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libstd-3aedbc8e8c0e1607.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libpanic_unwind-43c077dbaac5c651.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libobject-4cacc5fa8a27ecf8.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libmemchr-64f6783c29437be0.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libaddr2line-686d787984217b59.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libgimli-514530b69e03e0ef.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_demangle-6d8857037a7ba1ac.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libstd_detect-87900d26b9dcad12.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libhashbrown-e7a3685c0bbd0c42.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_std_workspace_alloc-38ecd594301af8ea.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libminiz_oxide-1ab977cb49e9f850.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libadler-193dd2a79ef160a9.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libunwind-8dea20a203d09b19.rlib -lunwind /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libcfg_if-b1bae776a6e5677d.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-d09271687f61e1fb.rlib -lc /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/liballoc-d02027f3bdb9876a.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_std_workspace_core-d87b0cd085418983.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libcore-8135530b14978e6d.rlib /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-08c46ed993ccb312.rlib -Wl,-Bdynamic -Wl,--eh-frame-hdr -Wl,-z,noexecstack -nostartfiles -L /rust/lib/rustlib/aarch64-unknown-linux-musl/lib -L /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained -o /target/aarch64-unknown-linux-musl/release/deps/myudev-4276b24e2eda4c6f -Wl,--gc-sections -static -no-pie -Wl,-z,relro,-z,now -Wl,-O1 -nodefaultlibs /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtend.o /rust/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtn.o
/usr/local/bin/../lib/gcc/aarch64-linux-musl/9.2.0/../../../../aarch64-linux-musl/bin/ld: cannot find -ludev
collect2: error: ld returned 1 exit status
error: could not compile `myudev` (bin "myudev") due to 1 previous error
我错过了什么?
从 musl 切换到
aarch64-unknown-linux-gnu
有效。