我正在尝试交叉编译一个简单的 Rust 程序,以使用 Raspberry Pi Zero 上的 ALSA 驱动程序录制声音,使用安装了libasound-dev
库的 Docker 容器内的波浪形板条箱。但是,链接器抱怨:
note: /opt/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/../lib/gcc/arm-linux-gnueabihf/4.8.3/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lasound
collect2: error: ld returned 1 exit status
似乎 Cargo 要求 rustc 将声音库与参数-Bdynamic" "-lasound"
动态链接。我如何告诉 Cargo 在哪里查找这些 ALSA 库?
更新:我将以下内容添加到我的 Cargo.toml 文件中,并将--features "alsa-backend"
添加到我的 cargo build
命令中,该命令似乎已经完成了构建:
[features]
alsa-backend = ["alsa"]
[dependencies]
alsa = { version = "0.2.1", optional = true }
它现在抛出:
note: /usr/lib/x86_64-linux-gnu/libasound.so: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
好的,所以它链接到 libasound.so 的x86_64版本。我在 Docker 容器中键入了dpkg -L libasound-dev
,事实上,它列出了/usr/lib/x86_64-linux-gnu/libasound.so
而不是 ARM 版本。
如何告诉 Raspbian Docker 容器链接到 ARM 版本的 libasound.so
?
解决方案:
- 将 libasound-dev 的 armhf 版本安装到你的 Raspbian docker 镜像中:
apt-get install libasound-dev -y
apt-get install libasound-dev:armhf -y
(如果你只安装 libasound-dev:armhf
,它会抱怨alsa-sys
链接器错误。
- 将
alsa
依赖项添加到 Cargo.toml:
[dependencies]
alsa = { version = "0.2.1", optional = true }
wavy = { path = "./wavy" }
- 在 Cargo.toml 中设置
alsa-backend
标志:
[features]
alsa-backend = ["alsa"]
将
--features "alsa-backend"
传递到cargo build --target arm-unknown-linux-gnueabihf
(应应用目标(告诉 rustc 在
.cargo/config
中使用 armhf 版本:
[build]
[target.arm-unknown-linux-gnueabihf.libasound]
linker = "arm-linux-gnueabihf-gcc"
rustc-link-lib = ["libasound"]
rustc-link-search = ["/usr/lib/arm-linux-gnueabihf"]
(根据它的链接顺序,它可能会尝试使用 x86 版本而不是 armhf 版本。