如果我尝试使用 `alloc` 板条箱来使用某些东西,我的 `avr_hal` rust 项目无法与 `libc_alloc` 链接

问题描述 投票:0回答:1

我的问题:当我尝试

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

  2. 奔跑

    cargo generate --git https://github.com/rahix/avr-hal-template.git --name avrgcc-linking-failed-mre
    (我用的是Uno)

  3. 添加

    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
  2. 中的部门

如果您不想使用 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. 构建开发容器

  2. 内置开发容器

    cd avrgcc-linking-failed-mre
    ,
    cargo build
    |
    cargo build --release

rust embedded avr-gcc
1个回答
0
投票

那是因为

alloc
在 avr micro 上不可用,对它们进行动态分配也不是一个好主意。因此,解决方案是从
alloc
列表和所有依赖项中删除
build-std
以及任何使用它的内容。

© www.soinside.com 2019 - 2024. All rights reserved.