
大家好,我们是eosBLACK团队。
2018年8月15日发布v1.2.0时,有传言称协议构建脚本,以EOSIO为基础的eosiocpp v1.3.0版本开始不再使用。此外,在9月20日,EOSIO发布EOSIO v1.3.0时曾正式介绍过使协议的制定更加容易的EOSIO.CDT。这次我们就来了解一下这个工具包有什么样的变化。
参考 : https://github.com/EOSIO/eosio.cdt
虽然eosiocpp不会马上消失,但是,将来的改进会在EOSIO.CDT上进行,所以最好是先把开发环境移过去。
这篇文章是在Windows10的wsl上安装友帮拓16.04后,以eosio.cdt
v1.2.0基础制作而成的。
安装
●复制来源
先复制来源,在Tag中校验v1.2.0

●构建及安装方法
Core symbol指定为“EOS”

安装路径是“/usr/local/eosio.cdt/”。“/usr/local/bin”上产生和指令相关的软链接。最大的不同就是eosio-cpp替代了eosiocpp。下面就通过实际事例了解下具体有哪些不同。
用简单的事例确认一下。
例如下面这一简单的协议例子。
mytest.hpp

mytest.cpp

为了和以前的方式做比较,先用eosiocpp构建一下

输出的mytest.wasm的大小是3,059字节。ABI输出也很顺利。这次我们用一下今天的主人公eosio-cpp。

输出的mytest.was的大小是2,392字节。后面的内容也会提到,传输相同的-O3 Option时,可以看到编译结果明显减少。在EOSIO,协议的大小和费用有直接的联系,因此,有理由按照变小的程度移走。问题是ABI文件,可以看出它是空的,无法输出任何东西。这是因为eosio-abigen无法识别之前方式的“注释”。先让我们创建一下ABI吧。
再详细了解一下
●创建ABI
如果是以前方式的eosiocpp,Action或Table可做如下注释后输出ABI信息

变更后的eosio-cpp可用C++11的attribute记述拟要输出的ABI,

或

根据变更后的规则对前面有问题的mytest.hpp做如下修改

重新构建后可以看到输出正常的ABI。
●ABI创建深化
在“~/eosio.cdt/example/abigen_test/test.cpp”创建ABI的方法如下事例。
编译后分不同情况进行了整理。
首先看一下结构Action定义

输出结果如下

下面是明示继承的结构名称的情况。 操作/表类型可以使用32个字符串('. 12345abcdefghiklmnopqrstuvwxyz '),并被编码为64位整数。最多可以创建13个字符。最多12位可以用于32个字符串。第13个字符只能写成“1-5”和“a-j”。但采用不同的内部代码和外部Interface的名称,这种境况就会减少。结构声明和ABI的曝光名称不同时,可明示如下

输出结果如下

现在看一下函数的Action定义方法。这个是输出时明示Action名称的例子

输出结果如下

直接使用定义名称的例子。传达的因子值也是前面定义的test_c类,这个也值得注意

最后是Table定义。所举事例是以相同的结构定义两个不同的多指标 Table

输出结果如下

下面是Table名称的例子

输出结果如下。

确认动作
eosio.cdt环境用一句话来讲就是对以wasm32为目标的clang工具链的解释器。也可以说,是把以Shell脚本提供的eosiocpp变更为C++。在构建过程中,因相关的目录,尤其是Boost的include路径等归属于现在的系统,因此编译后的eosio-cpp、eosio-ld等执行文件很难在其他计算机上使用。因此,掌握eosio-cpp给实际clang工具链传达什么参数也会是很有意义的工作。
eosio-cpp的源可以看一下“~/eosio.cdt/build/EosioClang-prefix/src/EosioClang-build/eosio-cpp.cpp”。 “~/eosio.cdt/eosio_llvm/tools/clang/tools/extra/eosio_c_tool/eosio-cpp.cpp.in”文件在构建过程中根据本地环境做了改变。其余的eosio-ld.cpp、eosio-abigen.cpp等源也是一样。
协议源的Compile呼叫clang++ ,链接呼叫eosio-ld ,abi创建呼叫eosio-abigen。为了了解在各个阶段如何传达命令行,将Console输出了eosio-cpp和eosio-ld的instr、linkstr、genstr等等。

重新构建后编译了一下mytest.cpp。

前面也提到过,“-O3”传达最优化选项。
看一下eosio-abigen.cpp源,传达的大部分Option已经记录到源代码本身。因此,和以前的“eosiocpp -g”一样,为了只创建ABI,可使用如下简单的命令行。

但有一点比较奇怪,eosio-abigen路径已经在PATH上,可省略执行路径后会出现如下错误。

参数传达部分的话,现在仍在修改当中,应该很快就能完善。eosio-cpp的选项中最显眼的就是-I,这在使用外部头文件时起到指定路径的作用,也能以下面的用途使用。

这个Option当呼叫clang++时可以正常传达-I因子,

但在abigen就会省略掉。

eosio-xx命令中双en-dash “--”是直接将命令传达给工具链的选项。因此,也曾做过这样的尝试,

在编译(Compile)中可以正常传达,

但在abigen阶段,出乎意料地在前部分就会卡住。

因此,使用外部Header时,要使用上面提到的直接呼叫eosio-abigen的同时用双en-dash(--)追加选项的方法。

写完这篇文章时v1.2.1也发布了。据说对不能传输-I参数的问题也进行了修改。https://github.com/EOSIO/eosio.cdt/releases/tag/v1.2.1
结束语
Eosio还处于起步阶段,在eosiocpp上增加DASH后变成eosio-cpp过程也表现了其勇敢无畏的精神。总之,希望这篇文章能在开发过程中,给充满诸多变化的eosio 提供一个方向。
谢谢
eosBLACK团队
eosBLACK 社区:
eosBLACK 官网:http//eosblack.io
eosBLACK Koreos:http://koreos.io/eosBLACK
eosBLACK Medium: https://medium.com/@eosblack
eosBLACK steemit: https://steemit.com/@eosblack
eosBLACK Facebook: https://www.facebook.com/eosBLACKTeam
eosBLACK twitter: https://twitter.com/EOSBLACK_IO
eosBLACK 电报群(韩文): https://t.me/eosBLACK_Korea
eosBLACK 电报群(英文): https://t.me/eosBLACK_English
白皮书(中文):https://www.eosblack.io/whitepaper/eosBLACK_Whitepaper_CN.pdf
白皮书 (韩文): http://bitly.kr/nap2
白皮书 (英文) : http://bitly.kr/MOsA

网友评论