You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装Rust的Inari库遇编译错误,添加RUSTFLAGS后问题未解决

Rust库Inari编译失败问题解决

我是Rust新手,安装区间算术库Inari时遇到编译错误,错误信息如下:

Compiling inari v1.0.0
error: RUSTFLAGS='-Ctarget-cpu=haswell' or later is required. See https://doc.rust-lang.org/rustc/codegen-options/#target-cpu
   --> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/inari-1.0.0/src/simd/x86_64.rs:174:9
    |
174 | ...   compile_error!("RUSTFLAGS='-Ctarget-cpu=haswell' or later is required. See https://doc.rust-lang.org/rustc/codegen-options/#target-cpu...
    |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0425]: cannot find function `add_ru` in this scope
  --> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/inari-1.0.0/src/arith.rs:25:21
   |
25 |         Self { rep: add_ru(x, y) }
   |                     ^^^^^^ help: a function with a similar name exists: `add_rn`
   |

...

我尝试在Cargo.toml中添加以下配置,但问题仍未解决:

[build]
rustflags = ["-Ctarget-cpu=haswell"]
rustdocflags = ["-Ctarget-cpu=haswell"]

复现步骤

  • 使用cargo new project-name创建新项目
  • 执行cargo add inari添加依赖
  • 运行cargo run启动项目

环境信息

  • cargo 1.62.1 (a748cf5a3 2022-06-08)
  • rustc 1.62.1 (e092d0b6b 2022-07-16)
  • Ubuntu 20.04.3 LTS(运行在WSL中)

解决方法

方法1:临时通过环境变量设置

运行命令时直接指定RUSTFLAGS:

RUSTFLAGS='-C target-cpu=haswell' cargo run

方法2:项目级永久配置

在项目根目录创建.cargo/config.toml文件(注意不是修改Cargo.toml),添加以下内容:

[build]
rustflags = ["-Ctarget-cpu=haswell"]

说明:第二个add_ru找不到的错误是因为第一个编译错误中断了SIMD相关代码的生成,解决target-cpu的问题后,这个错误会自动消失。

内容的提问来源于stack exchange,提问作者Nejc Jezersek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 10:54:21