VS Code中Solidity编译器版本切换失败问题求助
问题:VS Code中Solidity合约编译版本不匹配问题
我是编程新手,希望得到易懂的简单解答。我正将代码从Remix迁移到VS Code,迁移的合约代码如下:
// SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.9.0; contract SimpleStorage { uint256 public favoriteNumber; struct People { uint256 favoriteNumber; string name; } People[] public people; mapping(string => uint256) public nameTofavoriteNumber; function store(uint256 _favoriteNumber) public { favoriteNumber = _favoriteNumber; } function retrieve() public view returns(uint256) { return favoriteNumber; } function addPerson(string memory _name, uint256 _favoriteNumber) public{ people.push(People(_favoriteNumber,_name )); nameTofavoriteNumber[_name] = _favoriteNumber; } }
编译时出现错误:
Source file requires different compiler version (current compiler is 0.4.17+commit.bdeb9e52.Emscripten.clang - note that nightly builds are considered to be strictly less than the released versionsolc
我尝试右键错误选择“Solidity:Change global compiler version(Remote)”切换多个版本,但工作区仍保持0.4.17版本;也通过File>Preferences>Setting修改Solidity: Compile Using Remote Version和Solidity: Compile Using Local Version为v0.6.0+commit.26b70077,问题仍未解决。
解决方法
- 优先配置工作区版本:VS Code的工作区设置会覆盖全局设置,在当前项目的
.vscode/settings.json文件中指定版本即可:- 若项目没有
.vscode文件夹,新建一个;再在其中创建settings.json文件 - 写入以下内容:
{ "solidity.compileUsingRemoteVersion": "v0.6.0+commit.26b70077" }
- 若项目没有
- 清除编译器缓存:打开命令面板(Ctrl+Shift+P),执行
Solidity: Clear compiler cache and recompile命令,再重新编译合约 - 更新Solidity插件:确保VS Code中的Solidity插件是最新版本,旧版本可能存在版本切换的bug
内容的提问来源于stack exchange,提问作者Warlock
相关产品推荐
相关产品推荐

