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

使用Anchor框架开发Solana Web3应用时遇`+solana`命令错误

问题描述

我尝试使用Rust语言结合Solana的Anchor框架开发Web3应用,执行以下命令创建项目:

$ anchor init hello-world
$ cd ./hello-world

运行anchor build或anchor test时,出现如下错误:

error: no such command: `+solana`

    Cargo does not handle `+toolchain` directives.
    Did you mean to invoke `cargo` through `rustup` instead?

当前使用的工具版本如下:

rustc --version
rustc 1.71.0 (8ede3aae2 2023-07-12) (built from a source tarball)

rustup --version
rustup 1.26.0 (5af9b9484 2023-04-05)

cargo --version
cargo 1.71.0 (cfd3bbd8f 2023-06-08)

solana --version
solana-cli 1.16.8 (src:b19d7219; feat:3712769919, client:SolanaLabs)

anchor --version
anchor-cli 0.28.0
解决方案

这个错误的核心原因是Anchor默认依赖solana Rust工具链,但你的环境未正确配置该工具链,或者系统使用的是未通过rustup管理的cargo版本。按以下步骤修复:

  1. 安装并配置Solana指定的Rust工具链
    执行Solana官方初始化命令,自动配置适配的rustup工具链:

    solana-install init stable
    

    若手动配置,可执行:

    # 根据你的操作系统架构调整,比如macOS ARM64用aarch64-apple-darwin
    rustup toolchain install stable-x86_64-unknown-linux-gnu
    rustup override set stable-x86_64-unknown-linux-gnu
    
  2. 确保cargo由rustup管理
    检查当前cargo的路径:

    which cargo
    

    若输出不是~/.cargo/bin/cargo(类Unix系统),说明系统使用的是自带的cargo,需将rustup的bin目录加入环境变量:

    • 在bash/zsh配置文件(~/.bashrc或~/.zshrc)中添加:
      export PATH="$HOME/.cargo/bin:$PATH"
      
    • 重新加载配置:
      source ~/.bashrc # zsh用户执行source ~/.zshrc
      
  3. 验证工具链配置
    执行以下命令确认工具链已正确配置:

    rustup toolchain list
    

    输出中应包含stable工具链,且标注为默认或当前项目的override版本。

  4. 重新执行Anchor构建命令
    返回项目目录,重新运行:

    anchor build
    

内容的提问来源于stack exchange,提问作者Vikas Bansal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 15:43:10