Solidity 代码注释

Solidity 支持 c 风格和 c++ 风格的注释。

  • 单行注释://之后到行尾的文本,都被看作注释,编译器忽略此内容
  • 块注释:/* 与 */ 之间的文本被看作注释, 编译器忽略此内容

注释示例:

function getResult() public view returns(uint){
   // 这是一行注释,类似于c++中的注释

   /*
    * 这是多行注释
    * 类似于c语言中的注释
    */
   uint a = 1;
   uint b = 2;
   uint result = a + b;
   return result;
}

我们通常在.sol文件第一句加上注释 // SPDX-License-Identifier: MIT,用于注明软件包数据交换遵循的版本。

如果没有这一句,通常在编译的时候会给出一个警告 warning:SPDX license identifier not provided in source file.

报错原文:

SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: " to each source file. Use “SPDX-License-Identifier: UNLICENSED” for non-open-source code. Please see https://spdx.org for more information.

警告原因:

solidity 0.6.8 版本后引入了SPDX,使用时要在文件第一句加上SPDX-License-Identifier: 格式的语句。

解决方法:

在.sol文件第一句加上// SPDX-License-Identifier: MIT即可,使用其他许可的话从下面网址中选择把SimPL-3.0改掉就可以了。

SPDX许可列表网址:https://spdx.org/licenses/

例如:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SolidityTest{
    function helloWorld() public pure returns(string memory) {
        return "Hello World";
    }
}

Solidity ^0.6.8 以上版本引入了 SPDX 许可证,如果源码中未包含 SPDX 许可证说明,编译时会出现警告:Warning: SPDX license identifier not prov ...