to be
a problem slover

solidity版本管理工具solc-select介绍

概述

开发以太坊智能合约,本地编译时,如果本地的 solidity 编译器 solc 的版本与合约代码使用的版本不一致时,编译会失败。

比如说合约使用 0.5.16 版本,而编译器使用 0.7.4 版本。

编译的时候会报以下错误:

Error: Source file requires different compiler version (current compiler is 0.7.4+commit.3f05b770.Linux.g++) - note that nightly builds are considered to be strictly less than the released version
 --> StakingRewards.sol:5:1:
  |
5 | pragma solidity ^0.5.16;
  | ^^^^^^^^^^^^^^^^^^^^^^^^

但是如果你把 0.7.4 版本卸载,安装 0.5.16 版本,下次调试其他版本合约怎么办?难道又要卸载重装?显然不是一个解决办法。

所以我们需要一个 solc 的版本管理工具,可用安装多个 solc 版本,然后选择一个使用,随时可以切换,原理类似于 Java 的版本管理工具 jenvnodejs 的版本管理工具 nvm

经过一番对比,我觉得 solc-select 好用,下面做简单介绍。

安装

安装的时候会下载所有稳定版本,solc 应该也不大, 安装很快。

Linux

git clone https://github.com/crytic/solc-select.git
./solc-select/scripts/install.sh

脚本运行完后将 .solc-select 加入 PATH 中, 需要将以下语句加入 .bashrc 或者 .zshrc 文件末尾。

export PATH=$HOME/.solc-select:$PATH

然后 source rc 文件。

source ~/.zshrc

OS X (测试失败)

osx 我根据官网的安装指引,安装失败了(20201111 号测试),已提了issue

目前 commit id: 002b9a8be26533c2240e1265870449f629477e32

有兴趣自己试下。

docker pull trailofbits/solc-select
docker run --read-only -i --rm --entrypoint='/bin/sh' trailofbits/solc-select:latest -c 'cat /usr/bin/install.sh' | bash -e

验证安装

首先检查是否在 PATH 中

$ which solc
/home/shiming/.solc-select/solc

运行 solc --versions,查看可用的版本,出现版本列表就安装成功了

$ solc --versions
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
......

查看目前所用的版本

$ solc --version
solc, the solidity compiler commandline interface
Version: 0.4.0+commit.acd334c9.Linux.g++

切换版本

$ solc use 0.5.16
Now using version 0.5.16

切换到正确的版本就可以愉快的编译了!

升级

$ solc --upgrade

参考资料

solc-select github repo

赞(14) 打赏
欢迎转载,注明出处:刘世明的博客 » solidity版本管理工具solc-select介绍

评论 10

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    您好,我按您的步骤从linux系统上做了好几遍,每次做完都是输入which solc有显示,但是输入solc–version没有任何显示,请问可以加您个联系方式吗,我想把我做的的截图给您看一下,您看看是不是我中间步骤有什么问题,非常感谢!下面是我的邮箱。

    小草帽5年前 (2020-11-15)回复
    • 您好 请问您是怎么解决这个问题的

      sanc5年前 (2021-01-06)回复
  2. #2

    我在运行./solc-select/scripts/install.sh的时候出现下面的问题,
    Installing solc versions into /home/zxy/.solc-select/usr/bin
    ./install_solc.sh: line 81: curl: command not found
    您好,请问您知道搜这是哪里出了问题吗

    小草帽5年前 (2020-11-15)回复
    • 我也是这个问题。而且打开了那个文件之后,根本没有install.sh的文件

      1235年前 (2021-10-04)回复
      • 去github看官方最新文档,mac应该早就支持了

        Shiming5年前 (2021-10-09)回复
  3. #3

    问题解决了,谢谢!

    小草帽5年前 (2020-11-16)回复
  4. #4

    Thank you!!1

    DELGER99465年前 (2021-01-05)回复
  5. #5

    Our AI-powered traffic solution delivers engaged, keyword-specific visitors from your target locations, saving you money compared to expensive paid ad platforms. Contact us today.

    https://marketingaged.com/

    Gabrielle Appel2个月前 (02-04)回复
  6. #6

    What if liushiming.cn could tap into TikTok for genuine leads? Our AI-driven growth service zeros in on the ideal users—based on hashtags they use and accounts they follow—to boost your reach and send traffic back to you. We’ve had websites like yours see 100+ new leads in a month.
    Want to make TikTok work for liushiming.cn? Watch this short video for more info: https://www.youtube.com/shorts/sfVup2NhPQ4

    Lan Picton2周前 (03-10)回复
  7. #7

    What if liushiming.cn could leverage TikTok for genuine leads? Our AI-driven growth service targets the right users—based on hashtags they use and people they watch—to supercharge your reach and push traffic back to you. We’ve had similar websites see 100+ leads in 30 days.
    Ready to make TikTok work for liushiming.cn? Get more information here: https://boostgrow.co/

    Ted Lafountain4天前回复

觉得文章有用就打赏一下作者

支付宝扫一扫打赏

微信扫一扫打赏