未找到目标 thumbv7em-none-eabihf 的 sin()、cos()、log10()浮点数

问题描述

我正在使用 Rust 1.51 和这个最小的箱子:

#![no_std]

fn main() {
    let a = 2.0.cos();
}

我用 cargo check --target thumbv7em-none-eabihf 构建它,编译器抱怨这条消息:no method named 'cos' found for type '{float}' in the current scopesin()log10() 相同。

我找到了 https://docs.rust-embedded.org/cortex-m-quickstart/cortex_m_quickstart/ 并且我希望目标 thumbv6m-none-eabithumbv7em-none-eabi 出现上述消息,但对于支持 FPU 的 thumbv7em-none-eabihf 不会。

我该如何解决这个问题?

解决方法

在 Rust 1.51(及更低版本)中,sincoslog10 等函数不是核心库 (core::) 的一部分,而只是标准库 ( std::),因此它们不可用。

一个实用的解决方案是使用 crate libm,它为 no_std 环境提供典型的数学函数。

#![no_std]

fn main() {
    let a = libm::cosf(2.0);
}

见: