我的`avr_hal` rust项目无法链接到`libc_alloc`,如果我尝试使用`alloc` crate

tzcvj98z  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(95)

我的问题:当我尝试cargo build我的avr_hal项目,它抛出error: linking with avr-gcc failed: exit status: 1
完整错误文本(mre):

WARN rustc_codegen_ssa::back::link Linker does not support -no-pie command line option. Retrying without.
error: linking with `avr-gcc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/usr/local/rustup/toolchains/nightly-2023-08-08-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/vscode/vscode-server/bin/linux-x64/74f6148eb9ea00507ec113ec51c489d6ffb4b771/bin/remote-cli:/usr/local/cargo/bin:/usr/local/cargo/bin:/usr/local/cargo/bin:/usr/local/cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/vscode/.local/bin" VSLANG="1033" "avr-gcc" "-mmcu=atmega328p" "/tmp/rustcfZfdOa/symbols.o" "/workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps/avrgcc_linking_failed_mre-4f3e20c93dc596f3.arduino_hal-6541d5c2c9fe98bb.arduino_hal.fa13f7103336eb93-cgu.0.rcgu.o.rcgu.o" "-Wl,--as-needed" "-L" "/workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps" "-L" "/workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/debug/deps" "-L" "/usr/local/rustup/toolchains/nightly-2023-08-08-x86_64-unknown-linux-gnu/lib/rustlib/avr-atmega328p/lib" "-Wl,-Bstatic" "/workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps/libcompiler_builtins-400b15d51260279e.rlib" "-Wl,-Bdynamic" "-lgcc" "-Wl,-z,noexecstack" "-L" "/usr/local/rustup/toolchains/nightly-2023-08-08-x86_64-unknown-linux-gnu/lib/rustlib/avr-atmega328p/lib" "-o" "/workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps/avrgcc_linking_failed_mre-4f3e20c93dc596f3.elf" "-Wl,--gc-sections"
  = note: /workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps/avrgcc_linking_failed_mre-4f3e20c93dc596f3.arduino_hal-6541d5c2c9fe98bb.arduino_hal.fa13f7103336eb93-cgu.0.rcgu.o.rcgu.o: In function `_$LT$libc_alloc..LibcAlloc$u20$as$u20$core..alloc..global..GlobalAlloc$GT$::alloc::hcb8d6e5a6803e71c':
          /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/libc_alloc-1.0.5/src/lib.rs:35: undefined reference to `memalign'
          /workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps/avrgcc_linking_failed_mre-4f3e20c93dc596f3.arduino_hal-6541d5c2c9fe98bb.arduino_hal.fa13f7103336eb93-cgu.0.rcgu.o.rcgu.o: In function `alloc::raw_vec::finish_grow::h0af711d7dce70706':
          arduino_hal.fa13f7103336eb93-cgu.0:(.text._ZN5alloc7raw_vec11finish_grow17h0af711d7dce70706E+0xb2): undefined reference to `memalign'
          collect2: error: ld returned 1 exit status
          
  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

这是什么原因造成的,如何解决?(在一般情况下使用avr_hal时,是否可以定义全局分配器?)

复制步骤(mre)

1.安装cargo-generate-cargo install cargo-generate
1.运行cargo generate --git https://github.com/rahix/avr-hal-template.git --name avrgcc-linking-failed-mre(我使用Uno)
1.添加avrgcc-linking-failed-mre/.cargo/config.toml

[build]
target = "avr-specs/avr-atmega328p.json"

[unstable]
build-std = ["core", "alloc"]

1.添加avrgcc-linking-failed-mre/src/main.rs

#![no_std]
#![no_main]

use libc_alloc::LibcAlloc;
use panic_halt as _;

extern crate alloc;

#[arduino_hal::entry]
fn main() -> ! {
    "123".replace("1", "4");
    loop {}
}

#[global_allocator]
static ALLOCATOR: LibcAlloc = LibcAlloc;

1.将libc_alloc = "1.0.5"添加到avrgcc-linking-failed-mre/Cargo.toml中的deps
如果不想使用devcontainers,请参阅avr_hal的快速入门
1.添加.devcontainer/devcontainer.json

{
    "name": "avrgcc_linking_fail_mre_devcontainer",
    "dockerFile": "Dockerfile"
}

1.添加.devcontainer/Dockerfile

FROM mcr.microsoft.com/devcontainers/rust:1-1-bullseye

RUN apt update

RUN apt install -y fish
RUN apt install -y avr-libc gcc-avr pkg-config avrdude libudev-dev build-essential

RUN cargo install cargo-generate

1.构建devcontainer
1.内置devcontainer cd avrgcc-linking-failed-mrecargo build| cargo build --release

yzuktlbb

yzuktlbb1#

这是因为alloc在avr micros上不可用,nor is it a good idea to have dynamic allocation on them链接文章列出并扩展了以下原因,这些原因也适用于Rust:

  • 运行时的非确定性分配。
  • 内存可能不可用。
  • 浪费空间。
  • 堆内存碎片。
  • 陡峭的执行开销。
  • 用户数据内存碎片。
  • 未指定分配的内存量。
  • “保存内存”和释放没有意义。
  • 内存泄漏。
  • 晃来晃去的指针。
  • 不确定的值。
  • 不确定的类型。

因此,非解决方案的解决方案是从build-std列表和所有依赖项中删除alloc和任何使用它的内容。

相关问题