使用Anchor构建项目遇多版本冲突问题求助
使用Anchor构建项目时执行anchor build报错:
package
solana-program v1.18.8cannot be built because it requires rustc 1.75.0 or newer, while the currently active rustc version is 1.72.0-dev
Either upgrade to rustc 1.75.0 or newer, or use
cargo update -p solana-program@1.18.8 --precise ver
whereveris the latest version ofsolana-programsupporting rustc 1.72.0-dev
但本地rustc --version显示为1.77.0,solana-cli --version为1.18.1,执行cargo-build-sbf --version却返回:
solana-cargo-build-sbf 1.18.1
platform-tools v1.39
rustc 1.72.0
已更新rustup toolchain但问题未解决,尝试执行cargo install solana-cargo-build-sbf --force时出现:
Updating crates.io index
error: could not findsolana-cargo-build-sbfin registrycrates-iowith version `=1.19.0
清理Solana工具链缓存
执行以下命令删除旧的工具链缓存,避免版本冲突:sudo rm -rf ~/.cache/solana/ rm -rf ~/.local/share/solana/install/active_release/重新安装匹配版本的Solana工具链
因为你的solana-cli版本是1.18.1,直接通过官方工具重新初始化对应版本的环境,它会自动安装适配的sbf工具链和rust版本:solana-install init 1.18.1执行完成后重启终端,再检查
cargo-build-sbf --version,确认rust版本已同步。绑定项目到Solana SBF工具链
在项目根目录创建rust-toolchain.toml文件,内容如下,强制项目使用Solana提供的sbf工具链:[toolchain] channel = "solana-sbf"清理Anchor缓存后重新构建
先清理Anchor的编译缓存,再执行构建:anchor clean anchor build关于
solana-cargo-build-sbf安装错误的说明
该工具并非通过crates.io单独安装,而是随Solana CLI一起分发的,所以不要用cargo install命令安装它,通过上述solana-install命令即可获取对应版本的工具。
内容的提问来源于stack exchange,提问作者Osman Ghani Granday

