首页>智库>Solidity中文文档 v0.8.0版>安装Solidity编译器

安装Solidity编译器

安装Solidity编译器

版本

Solidity的版本遵循 语义化版本原则,作为发布版本的补充, 每日开发构建 (nightly development builds)也是可用的。这个每日开发构建不保证能正常工作,尽管尽了最大的努力,但仍可能包含未记录的和/或重大的改动。我们推荐使用最新的发布版本。下面的包安装程序将使用最新发布版本。

Remix

我们推荐使用 Remix 来开发简单合约和快速学习 Solidity。

Remix 可在线使用,而无需安装任何东西。如果你想离线使用,可按 https://github.com/ethereum/remix-live/tree/gh-pages 的页面说明下载 .zip 文件来使用。 该页面有进一步详细说明如何安装 Solidity 命令行编译器到你计算机上。如果你刚好要处理大型合约,或者需要更多的编译选项,那么你应该选择使用命令行编译器 solc。

译者注:Remix 还有一个 Remix-ide 本地版本。

npm / Node.js

使用 npm 可以便捷地安装Solidity编译器solcjs。但该 solcjs 程序的功能相对于本页下面的所有其他选项都要少。在 使用命令行编译器 一章中,我们假定你使用的是完整功能的编译器。 所以,如果你是从 npm 安装 solcjs ,就此打住,直接跳到 solc-js 去了解。

注意: solc-js 项目是利用 Emscripten 从 C++ 版的 solc 跨平台编译为 JavaScript 的,因此,可在 JavaScript 项目中使用 solcjs(如同 Remix)。 具体介绍请参考 solc-js 代码库。

npm install -g solc

注解

在命令行中,使用 solcjs 而非 solc 。 solcjs 的命令行选项同 solc 和一些工具(如 geth )是不兼容的,因此不要期望 solcjs 能像 solc 一样工作。

Docker

我们为编译器提供了最新的docker构建。 stable 仓库里的是已发布的版本,nightly 仓库则是在开发分支中的带有不稳定变更的版本。

docker run ethereum/solc:stable solc --version

目前,docker 镜像只含有 solc 的可执行程序,因此你需要额外的工作去把源代码和输出目录连接起来。

Linux 包

可在 solidity/releases 下载 Solidity 的二进制安装包。

对于 Ubuntu ,我们也提供 PPAs 。通过以下命令,可获取最新的稳定版本:

sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install solc

当然,你也可安装尝鲜的开发者版本:

sudo add-apt-repository ppa:ethereum/ethereum
sudo add-apt-repository ppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install solc

同时,也提供可安装 所有支持的Linux版本 下的 snap package 。通过以下命令,可获取最新的稳定版本:

sudo snap install solc

或者,如果你想测试 develop 分支下的最新变更,可通过如下方式安装开发者版本:

sudo snap install solc --edge

同样,Arch Linux 也有提供安装包,但仅限于最新的开发者版本:

pacman -S solidity

Gentoo Linux has an Ethereum overlay that contains a Solidity package. After the overlay is setup, solc can be installed in x86_64 architectures by:

emerge dev-lang/solidity

macOS Packages

我们通过 Homebrew 从源码构建Solidity 编译器,预构建的bottles暂时还不支持。

brew update
brew upgrade
brew tap ethereum/ethereum
brew install solidity

如果你需要特定版本的 Solidity ,你需要从 Github 上安装一个 Homebrew formula。 你可查阅 solidity.rb 在 Github的提交记录 的提交记录,去寻找包含 solidity.rb 文件改动的特殊提交。然后使用 brew 进行安装:

复制提交记录的你想要安装版本的提交记录 Hash ,下载(checkout)到本地。

git clone https://github.com/ethereum/homebrew-ethereum.git
cd homebrew-ethereum
git checkout <提交记录 hash>

使用 brew 安装:

brew unlink solidity
# 例如安装 0.4.8
brew install solidity.rb

冻结的二进制版本

We maintain a repository containing static builds of past and current compiler versions for all supported platforms at solc-bin. This is also the location where you can find the nightly builds.

The repository is not only a quick and easy way for end users to get binaries ready to be used out-of-the-box but it is also meant to be friendly to third-party tools:

  • The content is mirrored to https://binaries.soliditylang.org where it can be easily downloaded over HTTPS without any authentication, rate limiting or the need to use git.
  • Content is served with correct Content-Type headers and lenient CORS configuration so that it can be directly loaded by tools running in the browser.
  • Binaries do not require installation or unpacking (with the exception of older Windows builds bundled with necessary DLLs).
  • We strive for a high level of backwards-compatibility. Files, once added, are not removed or moved without providing a symlink/redirect at the old location. They are also never modified in place and should always match the original checksum. The only exception would be broken or unusable files with a potential to cause more harm than good if left as is.
  • Files are served over both HTTP and HTTPS. As long as you obtain the file list in a secure way (via git, HTTPS, IPFS or just have it cached locally) and verify hashes of the binaries after downloading them, you do not have to use HTTPS for the binaries themselves.

The same binaries are in most cases available on the Solidity release page on Github. The difference is that we do not generally update old releases on the Github release page. This means that we do not rename them if the naming convention changes and we do not add builds for platforms that were not supported at the time of release. This only happens in solc-bin.

The solc-bin repository contains several top-level directories, each representing a single platform. Each one contains a list.json file listing the available binaries. For example in emscripten-wasm32/list.json you will find the following information about version 0.7.4:

{
  "path": "solc-emscripten-wasm32-v0.7.4+commit.3f05b770.js",
  "version": "0.7.4",
  "build": "commit.3f05b770",
  "longVersion": "0.7.4+commit.3f05b770",
  "keccak256": "0x300330ecd127756b824aa13e843cb1f43c473cb22eaf3750d5fb9c99279af8c3",
  "urls": [
    "bzzr://16c5f09109c793db99fe35f037c6092b061bd39260ee7a677c8a97f18c955ab1",
    "dweb:/ipfs/QmTLs5MuLEWXQkths41HiACoXDiH8zxyqBHGFDRSzVE5CS"
  ]
}

This means that:

警告

Due to the strong backwards compatibility requirement the repository contains some legacy elements but you should avoid using them when writing new tools:

  • Use emscripten-wasm32/ (with a fallback to emscripten-asmjs/) instead of bin/ if you want the best performance. Until version 0.6.1 we only provided asm.js binaries. Starting with 0.6.2 we switched to WebAssembly builds with much better performance. We have rebuilt the older versions for wasm but the original asm.js files remain in bin/. The new ones had to be placed in a separate directory to avoid name clashes.
  • Use emscripten-asmjs/ and emscripten-wasm32/ instead of bin/ and wasm/ directories if you want to be sure whether you are downloading a wasm or an asm.js binary.
  • Use list.json instead of list.js and list.txt. The JSON list format contains all the information from the old ones and more.
  • Use https://binaries.soliditylang.org instead of https://solc-bin.ethereum.org. To keep things simple we moved almost everything related to the compiler under the new soliditylang.org domain and this applies to solc-bin too. While the new domain is recommended, the old one is still fully supported and guaranteed to point at the same location.

警告

The binaries are also available at https://ethereum.github.io/solc-bin/ but this page stopped being updated just after the release of version 0.7.2, will not receive any new releases or nightly builds for any platform and does not serve the new directory structure, including non-emscripten builds.

If you are using it, please switch to https://binaries.soliditylang.org, which is a drop-in replacement. This allows us to make changes to the underlying hosting in a transparent way and minimize disruption. Unlike the ethereum.github.io domain, which we do not have any control over, binaries.soliditylang.org is guaranteed to work and maintain the same URL structure in the long-term.

从源代码编译

预先安装环境 – 所有平台

以下是所有编译Solidity的依赖关系:

注解

Solidity versions prior to 0.5.10 can fail to correctly link against Boost versions 1.70+. A possible workaround is to temporarily rename <Boost install path>/lib/cmake/Boost-1.70.0 prior to running the cmake command to configure solidity.

Starting from 0.5.10 linking against Boost 1.70+ should work without manual intervention.

最低的编译器版本

The following C++ compilers and their minimum versions can build the Solidity codebase:

环境依赖条件 – macOS

在 macOS 中,需确保有安装最新版的 Xcode, Xcode 包含 Clang C++ 编译器, 而 Xcode IDE 和其他苹果 OS X 下编译 C++ 应用所必须的开发工具。 如果你是第一次安装 Xcode 或者刚好更新了 Xcode 新版本,则在使用命令行构建前,需同意 Xcode 的使用协议:

sudo xcodebuild -license accept

Solidity 在 OS X 下构建,必须 安装 Homebrew 包管理器来安装依赖。 如果你想从头开始,这里是 卸载 Homebrew 的方法

环境依赖条件 – Windows

在Windows下构建Solidity,需下载的依赖软件包:

软件备注
Visual Studio 2019 Build ToolsC++ 编译器
Visual Studio 2019 (Optional)C++ 编译器和开发环境

如果你已经有了 IDE,仅需要编译器和相关的库,你可以安装 Visual Studio 2019 Build Tools。

Visual Studio 2019 提供了 IDE 以及必要的编译器和库。所以如果你还没有一个 IDE 并且想要开发 Solidity,那么 Visual Studio 2019 将是一个可以使你获得所有工具的简单选择。

这里是一个在 Visual Studio 2019 Build Tools 或 Visual Studio 2019 中应该安装的组件列表:

  • Visual Studio C++ core features
  • VC++ 2019 v141 toolset (x86,x64)
  • Windows Universal CRT SDK
  • Windows 8.1 SDK
  • C++/CLI support

依赖的帮助脚本

在 macOS、Windows和其他 Linux 发行版上,有一个脚本可以“一键”安装所需的外部依赖库。本来是需要人工参与的多步操作,现在只需一行命令:

./scripts/install_deps.sh

Windows 下执行:

scripts\install_deps.ps1

请注意,后一个命令将在“deps“子目录中安装 boost 和“cmake“,而前一个命令将尝试在全局安装依赖项。

克隆代码库

执行以下命令,克隆源代码:

git clone --recursive https://github.com/ethereum/solidity.git
cd solidity

如果你想参与 Solidity 的开发, 你可分叉 Solidity 源码库后,用你个人的分叉库作为第二远程源:

git remote add personal git@github.com:[username]/solidity.git

注解

This method will result in a prerelease build leading to e.g. a flag being set in each bytecode produced by such a compiler. If you want to re-build a released Solidity compiler, then please use the source tarball on the github release page:

https://github.com/ethereum/solidity/releases/download/v0.X.Y/solidity_0.X.Y.tar.gz

(not the “Source code” provided by github).

Solidity 有 Git 子模块,需确保完全加载它们:

git submodule update --init --recursive

命令行构建

确保你已安装外部依赖(见上面)

Solidity 使用 CMake 来配置构建。你也许想要安装 `cache`_ 来加速重复构建,CMake自动进行这个工作。 Linux、macOS 和其他 Unix系统上的构建方式都差不多:

mkdir build
cd build
cmake .. && make

也有更简单的:

#note: 将安装 solc 和 soltest 到 usr/local/bin 目录
./scripts/build.sh

对于 Windows 执行:

mkdir build
cd build
cmake -G "Visual Studio 16 2019 Win64" ..

如果你想执行 ./scripts/install_deps.ps1 时使用你安装过的boost版本,可以添加参数 -DBoost_DIR="..\deps\boost\lib\cmake\Boost-*" 和 -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded 去调用 cmake.

这组指令的最后一句,会在 build 目录下创建一个 solidity.sln 文件,双击后,默认会使用 Visual Studio 打开。我们建议在VS上创建 RelWithDebugInfo 配置文件。

或者用命令创建:

cmake --build . --config RelWithDebInfo

CMake参数

如果你对 CMake 命令选项有兴趣,可执行 cmake .. -LH 进行查看。

SMT Solvers

Solidity can be built against SMT solvers and will do so by default if they are found in the system. Each solver can be disabled by a cmake option.

Note: In some cases, this can also be a potential workaround for build failures.

Inside the build folder you can disable them, since they are enabled by default:

# disables only Z3 SMT Solver.
cmake .. -DUSE_Z3=OFF

# disables only CVC4 SMT Solver.
cmake .. -DUSE_CVC4=OFF

# disables both Z3 and CVC4
cmake .. -DUSE_CVC4=OFF -DUSE_Z3=OFF

版本号字符串详解

Solidity 版本名包含四部分:

  • 版本号
  • 预发布版本号,通常为 develop.YYYY.MM.DD 或者 nightly.YYYY.MM.DD
  • 以 commit.GITHASH 格式展示的提交号
  • 由若干条平台、编译器详细信息构成的平台标识

如果本地有修改,则 commit 部分有后缀 .mod

这些部分按照 Semver 的要求来组合, Solidity 预发布版本号等价于 Semver 预发布版本号, Solidity 提交号和平台标识则组成 Semver 的构建元数据。

发行版样例:0.4.8+commit.60cc1668.Emscripten.clang.

预发布版样例: 0.4.9-nightly.2017.1.17+commit.6ecb4aa3.Emscripten.clang

版本信息详情

在版本发布之后,补丁版本号会增加,因为我们假定只有补丁级别的变更会在之后发生。当变更被合并后,版本应该根据semver和变更的剧烈程度进行调整。最后,发行版本总是与当前每日构建版本的版本号一致,但没有 prerelease 指示符。

例如:

  1. 0.4.0 版本发布
  2. 从现在开始,每晚构建为 0.4.1 版本
  3. 引入非破坏性变更 —— 不改变版本号
  4. 引入破坏性变更 —— 版本跳跃到 0.5.0
  5. 0.5.0 版本发布

该方式与 version pragma 一起运行良好。

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索