Using the compiler
Using the compiler
Using the Commandline Compiler
This section does not apply to solcjs, not even if it is used in commandline mode.
One of the build targets of the Solidity repository is solc
, the solidity commandline compiler. Using solc --help
provides you with an explanation of all options. The compiler can produce various outputs, ranging from simple binaries and assembly over an abstract syntax tree (parse tree) to estimations of gas usage. If you only want to compile a single file, you run it as solc --bin sourceFile.sol
and it will print the binary. If you want to get some of the more advanced output variants of solc
, it is probably better to tell it to output everything to separate files using solc -o outputDirectory --bin --ast --asm sourceFile.sol
.
Before you deploy your contract, activate the optimizer when compiling using solc --optimize --bin sourceFile.sol
. By default, the optimizer will optimize the contract assuming it is called 200 times across its lifetime. If you want the initial contract deployment to be cheaper and the later function executions to be more expensive, set it to --runs=1
. If you expect many transactions and do not care for higher deployment cost and output size, set --runs
to a high number.
The commandline compiler will automatically read imported files from the filesystem, but it is also possible to provide path redirects using prefix=path
in the following way:
This essentially instructs the compiler to search for anything starting with github.com/ethereum/dapp-bin/
under /usr/local/lib/dapp-bin
. solc
will not read files from the filesystem that lie outside of the remapping targets and outside of the directories where explicitly specified source files reside, so things like import "/etc/passwd";
only work if you add /=/
as a remapping.
An empty remapping prefix is not allowed.
If there are multiple matches due to remappings, the one with the longest common prefix is selected.
For security reasons the compiler has restrictions what directories it can access. Paths (and their subdirectories) of source files specified on the commandline and paths defined by remappings are allowed for import statements, but everything else is rejected. Additional paths (and their subdirectories) can be allowed via the --allow-paths /sample/path,/another/sample/path
switch.
If your contracts use libraries, you will notice that the bytecode contains substrings of the form __$53aea86b7d70b31448b230b20ae141a537$__
. These are placeholders for the actual library addresses. The placeholder is a 34 character prefix of the hex encoding of the keccak256 hash of the fully qualified library name. The bytecode file will also contain lines of the form // -> library name>
at the end to help identify which libraries the placeholders represent. Note that the fully qualified library name is the path of its source file and the library name separated by :
. You can use solc
as a linker meaning that it will insert the library addresses for you at those points:
Either add --libraries "file.sol:Math:0x1234567890123456789012345678901234567890 file.sol:Heap:0xabCD567890123456789012345678901234567890"
to your command to provide an address for each library or store the string in a file (one library per line) and run solc
using --libraries fileName
.
If solc
is called with the option --link
, all input files are interpreted to be unlinked binaries (hex-encoded) in the __$53aea86b7d70b31448b230b20ae141a537$__
-format given above and are linked in-place (if the input is read from stdin, it is written to stdout). All options except --libraries
are ignored (including -o
) in this case.
If solc
is called with the option --standard-json
, it will expect a JSON input (as explained below) on the standard input, and return a JSON output on the standard output. This is the recommended interface for more complex and especially automated uses.
The library placeholder used to be the fully qualified name of the library itself instead of the hash of it. This format is still supported by solc --link
but the compiler will no longer output it. This change was made to reduce the likelihood of a collision between libraries, since only the first 36 characters of the fully qualified library name could be used.
Setting the EVM version to target
When you compile your contract code you can specify the Ethereum virtual machine version to compile for to avoid particular features or behaviours.
Compiling for the wrong EVM version can result in wrong, strange and failing behaviour. Please ensure, especially if running a private chain, that you use matching EVM versions.
On the command line, you can select the EVM version as follows:
In the standard JSON interface, use the "evmVersion"
key in the "settings"
field:
Target options
Below is a list of target EVM versions and the compiler-relevant changes introduced at each version. Backward compatibility is not guaranteed between each version.
homestead
(oldest version)tangerineWhistle
gas cost for access to other accounts increased, relevant for gas estimation and the optimizer.
all gas sent by default for external calls, previously a certain amount had to be retained.
spuriousDragon
gas cost for the
exp
opcode increased, relevant for gas estimation and the optimizer.
byzantium
(default)opcodes
returndatacopy
,returndatasize
andstaticcall
are available in assembly.the
staticcall
opcode is used when calling non-library view or pure functions, which prevents the functions from modifying state at the EVM level, i.e., even applies when you use invalid type conversions.it is possible to access dynamic data returned from function calls.
revert
opcode introduced, which means thatrevert()
will not waste gas.
constantinople
(still in progress)opcodes
shl
,shr
andsar
are available in assembly.shifting operators use shifting opcodes and thus need less gas.
Compiler Input and Output JSON Description
The recommended way to interface with the Solidity compiler especially for more complex and automated setups is the so-called JSON-input-output interface. The same interface is provided by all distributions of the compiler.
The fields are generally subject to change, some are optional (as noted), but we try to only make backwards compatible changes.
The compiler API expects a JSON formatted input and outputs the compilation result in a JSON formatted output.
The following subsections describe the format through an example. Comments are of course not permitted and used here only for explanatory purposes.
Input Description
Output Description
Error types
JSONError
: JSON input doesn’t conform to the required format, e.g. input is not a JSON object, the language is not supported, etc.IOError
: IO and import processing errors, such as unresolvable URL or hash mismatch in supplied sources.ParserError
: Source code doesn’t conform to the language rules.DocstringParsingError
: The NatSpec tags in the comment block cannot be parsed.SyntaxError
: Syntactical error, such ascontinue
is used outside of afor
loop.DeclarationError
: Invalid, unresolvable or clashing identifier names. e.g.Identifier not found
TypeError
: Error within the type system, such as invalid type conversions, invalid assignments, etc.UnimplementedFeatureError
: Feature is not supported by the compiler, but is expected to be supported in future versions.InternalCompilerError
: Internal bug triggered in the compiler - this should be reported as an issue.Exception
: Unknown failure during compilation - this should be reported as an issue.CompilerError
: Invalid use of the compiler stack - this should be reported as an issue.FatalError
: Fatal error not processed correctly - this should be reported as an issue.Warning
: A warning, which didn’t stop the compilation, but should be addressed if possible.
Last updated