安装 Solidity 编译器
版本控制
Solidity 版本遵循 语义版本控制。此外,主版本为 0 的补丁级发布(即 0.x.y)将不包含重大更改。这意味着在版本 0.x.y 下编译的代码预计可以在 0.x.z 下编译,其中 z > y。
除了发布版,我们还提供 **每日开发版本**,以便开发者可以轻松尝试即将推出的功能并提供早期反馈。但是请注意,尽管每日开发版本通常非常稳定,但它们包含来自开发分支的最新代码,并且不能保证始终有效。尽管我们尽了最大努力,但它们可能包含未记录或损坏的更改,这些更改不会成为实际发布版的一部分。它们不适合生产使用。
部署合约时,应使用最新版本的 Solidity。这是因为重大更改以及新功能和错误修复会定期引入。我们目前使用 0.x 版本号 来表明这种快速的变化速度.
Remix
我们推荐 Remix 用于小型合约以及快速学习 Solidity。
在线访问 Remix,无需安装任何东西。如果您想在没有互联网连接的情况下使用它,请访问 https://github.com/ethereum/remix-live/tree/gh-pages#readme 并按照该页面上的说明操作。Remix 也是测试每日开发版本而无需安装多个 Solidity 版本的便捷选项。
本页面上的其他选项详细介绍了在您的计算机上安装命令行 Solidity 编译器软件。如果您正在处理较大的合约或需要更多编译选项,请选择命令行编译器。
npm / Node.js
使用 npm
可以方便地安装 Solidity 编译器 solcjs
。程序 solcjs 的功能比本页面下方描述的访问编译器的方式要少。使用命令行编译器 文档假设您使用的是功能齐全的编译器 solc
。关于 solcjs
的使用已在其自己的 仓库 中记录。
注意:solc-js 项目是使用 Emscripten 从 C++ solc 衍生出来的,这意味着两者使用相同的编译器源代码。solc-js 可以直接在 JavaScript 项目中使用(例如 Remix)。请参考 solc-js 仓库以获取说明。
npm install -g solc
注意
命令行可执行文件名为 solcjs
。
solcjs
的命令行选项与 solc
不兼容,并且工具(例如 geth
)期望 solc
的行为将无法与 solcjs
一起使用。
Docker
Solidity 构建的 Docker 镜像可使用 ethereum
组织的 solc
镜像获取。使用 stable
标签获取最新发布版,使用 nightly
标签获取 develop
分支中可能不稳定的更改。
Docker 镜像运行编译器可执行文件,以便您可以将所有编译器参数传递给它。例如,以下命令将拉取稳定版本的 solc
镜像(如果您还没有),并在新容器中运行它,并传递 --help
参数。
docker run ethereum/solc:stable --help
您可以在标签中指定发布版版本。例如
docker run ethereum/solc:stable --help
注意
特定编译器版本作为 Docker 镜像标签得到支持,例如 ethereum/solc:0.8.23。在这里,我们将传递 stable 标签而不是特定版本标签,以确保用户默认情况下获得最新版本,并避免出现版本过时的問題。
要使用 Docker 镜像在主机上编译 Solidity 文件,请挂载本地文件夹以进行输入和输出,并指定要编译的合约。例如
docker run -v /local/path:/sources ethereum/solc:stable -o /sources/output --abi --bin /sources/Contract.sol
您也可以使用标准 JSON 接口(建议在将编译器与工具一起使用时使用)。使用此接口时,只要 JSON 输入是自包含的(即它不引用必须通过 导入回调加载 的任何外部文件),则无需挂载任何目录。
docker run ethereum/solc:stable --standard-json < input.json > output.json
Linux 包
Solidity 的二进制包可在 solidity/releases 获取。
我们还为 Ubuntu 提供了 PPA,您可以使用以下命令获取最新的稳定版本
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 发行版提供了自己的包。这些包并非由我们直接维护,但通常由各自的包维护者保持最新。
例如,Arch Linux 为最新开发版本提供了 AUR 包:solidity 和 solidity-bin.
注意
请注意,AUR 包是用户生成的內容,并且是非官方包。使用它们时请谨慎。
还有一个 snap 包,但是,它 **目前未维护**。它可以在所有 支持的 Linux 发行版 中安装。要安装最新稳定版本的 solc
sudo snap install solc
如果您想帮助使用最新更改测试最新开发版本的 Solidity,请使用以下命令
sudo snap install solc --edge
注意
solc
snap 使用严格的隔离。这是 snap 包最安全的方式,但它也有一些限制,例如只能访问您 /home
和 /media
目录中的文件。有关更多信息,请访问 Demystifying Snap Confinement.
macOS 包
我们通过 Homebrew 分发 Solidity 编译器,作为一个从源代码构建的版本。目前不支持预构建的瓶子。
brew update
brew upgrade
brew tap ethereum/ethereum
brew install solidity
要安装最新版本的 0.4.x / 0.5.x Solidity,您也可以分别使用 brew install solidity@4
和 brew install solidity@5
。
如果您需要特定版本的 Solidity,可以从 Github 直接安装 Homebrew 公式。
复制您想要的版本的提交哈希并将其检出到您的机器上。
git clone https://github.com/ethereum/homebrew-ethereum.git
cd homebrew-ethereum
git checkout <your-hash-goes-here>
使用 brew
安装它
brew unlink solidity
# eg. Install 0.4.8
brew install solidity.rb
静态二进制文件
我们维护一个包含过去和当前编译器版本(针对所有支持平台)的静态构建的仓库,位于 solc-bin。您也可以在此处找到每日开发版本。
该仓库不仅是最终用户快速轻松地获取现成的二进制文件的简便方法,而且也是为了便于第三方工具使用
内容镜像到 https://binaries.soliditylang.org,您可以在其中轻松通过 HTTPS 下载,无需任何身份验证、速率限制或使用 git。
内容以正确的 Content-Type 标头和宽松的 CORS 配置提供,以便可以由在浏览器中运行的工具直接加载。
二进制文件不需要安装或解包(旧的 Windows 构建除外,它们与必要的 DLL 捆绑在一起)。
我们致力于高度的向后兼容性。一旦添加的文件,就不会删除或移动,除非在旧位置提供符号链接/重定向。它们也永远不会被就地修改,并且应该始终与原始校验和匹配。唯一的例外是损坏或无法使用的文件,如果保留它们,它们可能会造成比好处更多的伤害。
文件通过 HTTP 和 HTTPS 提供。只要您以安全的方式获取文件列表(通过 git、HTTPS、IPFS 或者只是将其缓存到本地)并验证下载后二进制文件的哈希值,您就不必对二进制文件本身使用 HTTPS。
在大多数情况下,相同的文件可在 GitHub 上的 Solidity 发布页面 获取。不同之处在于,我们通常不会更新 GitHub 发布页面上的旧发布版。这意味着如果命名约定发生更改,我们不会重命名它们,并且我们不会为当时不支持的平台添加构建。这只会发生在 solc-bin
中。
The solc-bin
repository contains several top-level directories, each representing a single platform. Each one includes 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",
"sha256": "0x2b55ed5fec4d9625b6c7b3ab1abd2b7fb7dd2a9c68543bf0323db2c7e2d55af2",
"urls": [
"bzzr://16c5f09109c793db99fe35f037c6092b061bd39260ee7a677c8a97f18c955ab1",
"dweb:/ipfs/QmTLs5MuLEWXQkths41HiACoXDiH8zxyqBHGFDRSzVE5CS"
]
}
This means that
You can find the binary in the same directory under the name solc-emscripten-wasm32-v0.7.4+commit.3f05b770.js. Note that the file might be a symlink, and you will need to resolve it yourself if you are not using git to download it or your file system does not support symlinks.
The binary is also mirrored at https://binaries.soliditylang.org/emscripten-wasm32/solc-emscripten-wasm32-v0.7.4+commit.3f05b770.js. In this case, git is not necessary and symlinks are resolved transparently, either by serving a copy of the file or returning a HTTP redirect.
The file is also available on IPFS at QmTLs5MuLEWXQkths41HiACoXDiH8zxyqBHGFDRSzVE5CS.
The file might in future be available on Swarm at 16c5f09109c793db99fe35f037c6092b061bd39260ee7a677c8a97f18c955ab1.
You can verify the integrity of the binary by comparing its keccak256 hash to
0x300330ecd127756b824aa13e843cb1f43c473cb22eaf3750d5fb9c99279af8c3
. The hash can be computed on the command-line using thekeccak256sum
utility provided by sha3sum or the keccak256() function from ethereumjs-util in JavaScript.You can also verify the integrity of the binary by comparing its sha256 hash to
0x2b55ed5fec4d9625b6c7b3ab1abd2b7fb7dd2a9c68543bf0323db2c7e2d55af2
.
Warning
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 toemscripten-asmjs/
) instead ofbin/
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 inbin/
. The new ones had to be placed in a separate directory to avoid name clashes.Use
emscripten-asmjs/
andemscripten-wasm32/
instead ofbin/
andwasm/
directories if you want to be sure whether you are downloading a wasm or an asm.js binary.Use
list.json
instead oflist.js
andlist.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 tosolc-bin
too. While the new domain is recommended, the old one is still fully supported and guaranteed to point at the same location.
Warning
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.
Building from Source
Prerequisites - All Operating Systems
The following are dependencies for all builds of Solidity
Software |
Notes |
---|---|
CMake (version 3.21.3+ on Windows, 3.13+ otherwise) |
Cross-platform build file generator. |
Boost (version 1.77+ on Windows, 1.65+ otherwise) |
C++ libraries. |
Command-line tool for retrieving source code. |
|
z3 (version 4.8.16+, Optional) |
For use with SMT checker. |
cvc4 (Optional) |
For use with SMT checker. |
注意
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 default build configuration requires a specific Z3 version (the latest one at the time the code was last updated). Changes introduced between Z3 releases often result in slightly different (but still valid) results being returned. Our SMT tests do not account for these differences and will likely fail with a different version than the one they were written for. This does not mean that a build using a different version is faulty. If you pass the -DSTRICT_Z3_VERSION=OFF
option to CMake, you can build with any version that satisfies the requirement given in the table above. If you do this, however, please remember to pass the --no-smt
option to scripts/tests.sh
to skip the SMT tests.
注意
By default, the build is performed in pedantic mode, which enables extra warnings and tells the compiler to treat all warnings as errors. This forces developers to fix warnings as they arise, so they do not accumulate “to be fixed later”. If you are only interested in creating a release build and do not intend to modify the source code to deal with such warnings, you can pass the -DPEDANTIC=OFF
option to CMake to disable this mode. Doing this is not recommended for general use but may be necessary when using a toolchain we are not testing with or trying to build an older version with newer tools. If you encounter such warnings, please consider reporting them.
Minimum Compiler Versions
The following C++ compilers and their minimum versions can build the Solidity codebase
Prerequisites - macOS
For macOS builds, ensure that you have the latest version of Xcode installed. This contains the Clang C++ compiler, the Xcode IDE, and other Apple development tools that are required for building C++ applications on OS X. If you are installing Xcode for the first time, or have just installed a new version, then you will need to agree to the license before you can do command-line builds.
sudo xcodebuild -license accept
Our OS X build script uses the Homebrew package manager for installing external dependencies. Here’s how to uninstall Homebrew, if you ever want to start again from scratch.
Prerequisites - Windows
You need to install the following dependencies for Windows builds of Solidity
Software |
Notes |
---|---|
C++ compiler |
|
Visual Studio 2019 (Optional) |
C++ compiler and dev environment. |
Boost (version 1.77+) |
C++ libraries. |
If you already have one IDE and only need the compiler and libraries, you could install Visual Studio 2019 Build Tools.
Visual Studio 2019 provides both IDE and necessary compiler and libraries. So if you have not got an IDE and prefer to develop Solidity, Visual Studio 2019 may be a choice for you to get everything setup easily.
Here is the list of components that should be installed in Visual Studio 2019 Build Tools or 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
We have a helper script which you can use to install all required external dependencies
scripts\install_deps.ps1
This will install boost
and cmake
to the deps
subdirectory.
Clone the Repository
To clone the source code, execute the following command
git clone --recursive https://github.com/ethereum/solidity.git
cd solidity
If you want to help develop Solidity, you should fork Solidity and add your personal fork as a second remote
git remote add personal [email protected]:[username]/solidity.git
注意
This method will result in a pre-release 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).
Command-Line Build
Be sure to install External Dependencies (see above) before build.
Solidity project uses CMake to configure the build. You might want to install ccache to speed up repeated builds. CMake will pick it up automatically. Building Solidity is quite similar on Linux, macOS, and other Unices
mkdir build
cd build
cmake .. && make
or even easier on Linux and macOS, you can run
#note: this will install binaries solc and soltest at usr/local/bin
./scripts/build.sh
Warning
BSD builds should work, but are untested by the Solidity team.
And for Windows
mkdir build
cd build
cmake -G "Visual Studio 16 2019" ..
In case you want to use the version of boost installed by scripts\install_deps.ps1
, you will additionally need to pass -DBoost_DIR="deps\boost\lib\cmake\Boost-*"
and -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded
as arguments to the call to cmake
.
这将导致在该构建目录中创建 **solidity.sln**。 双击该文件将启动 Visual Studio。 我们建议构建 **Release** 配置,但所有其他配置都可以正常工作。
或者,您可以在命令行上为 Windows 构建,如下所示
cmake --build . --config Release
CMake 选项
如果您想知道哪些 CMake 选项可用,请运行 cmake .. -LH
。
SMT 求解器
Solidity 可以针对 SMT 求解器构建,如果在系统中找到 SMT 求解器,则默认情况下会这样做。 每个求解器都可以通过 cmake
选项禁用。
注意:在某些情况下,这也可能是构建失败的潜在解决方法。
在构建文件夹中,您可以禁用它们,因为它们默认情况下是启用的
# 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
平台,其中包含任意数量的项目,包含有关平台和编译器的详细信息
如果有本地修改,则提交将以 .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 和更改的严重程度来增加版本。 最后,始终使用当前 nightly 构建的版本进行发布,但没有 prerelease
指定符。
示例
发布了 0.4.0 版本。
从现在开始,nightly 构建的版本为 0.4.1。
引入了非破坏性更改 -> 版本没有更改。
引入了破坏性更改 -> 版本升级到 0.5.0。
发布了 0.5.0 版本。
此行为与 版本 pragma 配合良好。