solidity編寫的以太坊智能合約可經過命令行編譯工具solc來進行編譯,成爲以太坊虛擬機中的代碼。solc編譯後最終部署到鏈上造成咱們所見到的各類智能合約。html
做爲一個solidity命令行編譯工具,咱們來看看官網都怎麼說solc。node
solc的安裝很簡單:git
npm install -g solc //或者 npm install -g solc-cli //或者 sudo apt-get install solc
安裝完成後咱們來看,solc --help
,solc --help命令顯示全部的solc命令選項。編譯器能夠生成各類輸出,好比最終的二進制合約文件、語法樹的彙編或者須要預計的要花費的gas等。solc --bin sourceFile.sol
,能夠編譯後輸出一個名爲sourceFile.sol的智能合約文件。若是你想從solc
得到更豐富的一些輸出變量,你可使用solc -o outputDirectory --bin --ast --asm sourceFile.sol
。github
你在部署以太坊智能合約以前能夠用solc --optimize --bin sourceFile.sol
優化一下。默認狀況下solc編譯器會幫你優化200次。你也能夠設置 --runs=1
,這樣就按照最小化的方式進行編譯,若是你但願屢次交易不太在意成本,那你能夠設置成你想要的次數:)。mongodb
命令行編譯器會自動讀取須要導入的文件,也能夠經過使用prefix = path
來指定路徑,例如:express
solc github.com/ethereum/dapp-bin/=/usr/local/lib/dapp-bin/ =/usr/local/lib/fallback file.sol
npm
這樣編譯器就會從指定目錄github.com/ethereum/dapp-bin/
下的/usr/local/lib/dapp-bin/
目錄開始搜索,若是沒有找到文件,它將查看/usr/local/lib/fallback
。solc將只讀取你指定的這兩個路徑的,所以像import "/etc/passwd";
必需要經過/=
從新映射才起做用。若是有多個匹配,則選擇具備最長公共前綴的進行匹配。json
出於安全上的考慮,編譯器限制了它能夠訪問的一些目錄。在命令行中指定的源文件的路徑(及其子目錄)和命令行指定的路徑外其餘全部內容都會被拒絕。--allow-paths /sample/path,/another/sample/path
來切換。安全
若是智能合約使用了libraries,你會注意到字節碼包含了__LibraryName______
的子字符串。您可使用solc做爲連接器,這意味着它將在這些點爲您插入庫地址。app
能夠經過添加庫--libraries "Math:0x12345678901234567890 Heap:0xabcdef0123456"
到您的命令,以提供每一個庫的地址,或者使用文件中的說明字符串(每行一個庫),並使用--libraries fileName
運行solc。
若是用選項--link
調用Solc,則全部輸入文件都被解釋爲未連接的二進制文件(HEX編碼),在上面給出的__LibraryName____
格式中,將其連接到適當地址(若是從stdin讀取輸入,則將其寫入stdout)。在這種狀況下,除了庫外,全部選項都被忽略(包括-o
)。
若是用--standard-json
調用SOLC,它就將標準的JSON輸入(以下所述),並返回JSON輸出。
#solc編譯器輸入輸出JSON描述
這些JSON格式經過編譯器API使用,能夠經過SOLC得到。內容都是能夠修改的,一些對象是可選的(如前所述),其目的是向後兼容。
編譯器的API須要一個JSON格式的輸入,而後以JSON格式輸出編譯結果。
注意不容許註釋。下面示例中的註釋,是官網爲了學習者更好的理解標註的。
{ // Required: Source code language, such as "Solidity", "serpent", "lll", "assembly", etc. language: "Solidity", // Required sources: { // The keys here are the "global" names of the source files, // imports can use other files via remappings (see below). "myFile.sol": { // Optional: keccak256 hash of the source file // It is used to verify the retrieved content if imported via URLs. "keccak256": "0x123...", // Required (unless "content" is used, see below): URL(s) to the source file. // URL(s) should be imported in this order and the result checked against the // keccak256 hash (if available). If the hash doesn't match or none of the // URL(s) result in success, an error should be raised. "urls": [ "bzzr://56ab...", "ipfs://Qma...", "file:///tmp/path/to/file.sol" ] }, "mortal": { // Optional: keccak256 hash of the source file "keccak256": "0x234...", // Required (unless "urls" is used): literal contents of the source file "content": "contract mortal is owned { function kill() { if (msg.sender == owner) selfdestruct(owner); } }" } }, // Optional settings: { // Optional: Sorted list of remappings remappings: [ ":g/dir" ], // Optional: Optimizer settings optimizer: { // disabled by default enabled: true, // Optimize for how many times you intend to run the code. // Lower values will optimize more for initial deployment cost, higher values will optimize more for high-frequency usage. runs: 200 }, evmVersion: "byzantium", // Version of the EVM to compile for. Affects type checking and code generation. Can be homestead, tangerineWhistle, spuriousDragon, byzantium or constantinople // Metadata settings (optional) metadata: { // Use only literal content and not URLs (false by default) useLiteralContent: true }, // Addresses of the libraries. If not all libraries are given here, it can result in unlinked objects whose output data is different. libraries: { // The top level key is the the name of the source file where the library is used. // If remappings are used, this source file should match the global path after remappings were applied. // If this key is an empty string, that refers to a global level. "myFile.sol": { "MyLib": "0x123123..." } } // The following can be used to select desired outputs. // If this field is omitted, then the compiler loads and does type checking, but will not generate any outputs apart from errors. // The first level key is the file name and the second is the contract name, where empty contract name refers to the file itself, // while the star refers to all of the contracts. // // The available output types are as follows: // abi - ABI // ast - AST of all source files // legacyAST - legacy AST of all source files // devdoc - Developer documentation (natspec) // userdoc - User documentation (natspec) // metadata - Metadata // ir - New assembly format before desugaring // evm.assembly - New assembly format after desugaring // evm.legacyAssembly - Old-style assembly format in JSON // evm.bytecode.object - Bytecode object // evm.bytecode.opcodes - Opcodes list // evm.bytecode.sourceMap - Source mapping (useful for debugging) // evm.bytecode.linkReferences - Link references (if unlinked object) // evm.deployedBytecode* - Deployed bytecode (has the same options as evm.bytecode) // evm.methodIdentifiers - The list of function hashes // evm.gasEstimates - Function gas estimates // ewasm.wast - eWASM S-expressions format (not supported atm) // ewasm.wasm - eWASM binary format (not supported atm) // // Note that using a using `evm`, `evm.bytecode`, `ewasm`, etc. will select every // target part of that output. Additionally, `*` can be used as a wildcard to request everything. // outputSelection: { // Enable the metadata and bytecode outputs of every single contract. "*": { "*": [ "metadata", "evm.bytecode" ] }, // Enable the abi and opcodes output of MyContract defined in file def. "def": { "MyContract": [ "abi", "evm.bytecode.opcodes" ] }, // Enable the source map output of every single contract. "*": { "*": [ "evm.bytecode.sourceMap" ] }, // Enable the legacy AST output of every single file. "*": { "": [ "legacyAST" ] } } } }
{ // Optional: not present if no errors/warnings were encountered errors: [ { // Optional: Location within the source file. sourceLocation: { file: "sourceFile.sol", start: 0, end: 100 ], // Mandatory: Error type, such as "TypeError", "InternalCompilerError", "Exception", etc. // See below for complete list of types. type: "TypeError", // Mandatory: Component where the error originated, such as "general", "ewasm", etc. component: "general", // Mandatory ("error" or "warning") severity: "error", // Mandatory message: "Invalid keyword" // Optional: the message formatted with source location formattedMessage: "sourceFile.sol:100: Invalid keyword" } ], // This contains the file-level outputs. In can be limited/filtered by the outputSelection settings. sources: { "sourceFile.sol": { // Identifier (used in source maps) id: 1, // The AST object ast: {}, // The legacy AST object legacyAST: {} } }, // This contains the contract-level outputs. It can be limited/filtered by the outputSelection settings. contracts: { "sourceFile.sol": { // If the language used has no contract names, this field should equal to an empty string. "ContractName": { // The Ethereum Contract ABI. If empty, it is represented as an empty array. // See https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI abi: [], // See the Metadata Output documentation (serialised JSON string) metadata: "{...}", // User documentation (natspec) userdoc: {}, // Developer documentation (natspec) devdoc: {}, // Intermediate representation (string) ir: "", // EVM-related outputs evm: { // Assembly (string) assembly: "", // Old-style assembly (object) legacyAssembly: {}, // Bytecode and related details. bytecode: { // The bytecode as a hex string. object: "00fe", // Opcodes list (string) opcodes: "", // The source mapping as a string. See the source mapping definition. sourceMap: "", // If given, this is an unlinked object. linkReferences: { "libraryFile.sol": { // Byte offsets into the bytecode. Linking replaces the 20 bytes located there. "Library1": [ { start: 0, length: 20 }, { start: 200, length: 20 } ] } } }, // The same layout as above. deployedBytecode: { }, // The list of function hashes methodIdentifiers: { "delegate(address)": "5c19a95c" }, // Function gas estimates gasEstimates: { creation: { codeDepositCost: "420000", executionCost: "infinite", totalCost: "infinite" }, external: { "delegate(address)": "25000" }, internal: { "heavyLifting()": "infinite" } } }, // eWASM related outputs ewasm: { // S-expressions format wast: "", // Binary format (hex string) wasm: "" } } } } }
continue
在for
循環以外使用。原文請訪問:solc
若是你但願立刻開始學習以太坊DApp開發,推薦訪問一個在線教程: