eos合约开发分为三步:
一、通过eosiocpp -n创建一个基本结构;
二、修改合约并通过eosiocpp -o编译合约为wast文件;
三、通过eosiocpp -g 生成abi文件。
通过查看eosiocpp的帮助文件可以查看具体说明
[lz@localhost tools]$ eosiocpp
Usage: /data/blockchain/eos/build/tools/eosiocpp -o output.wast contract.cpp [other.cpp ...]
OR
/data/blockchain/eos/build/tools/eosiocpp -n mycontract
OR
/data/blockchain/eos/build/tools/eosiocpp -g contract.abi types.hpp
Options:
-n | --newcontract [name]
Create a new contract in the [name] folder, based on the example contract
OR
-o | --outname [output.wast] [input.cpp ...]
Generate the wast output file based on input cpp files
OR
-g | --genabi contract.abi types.hpp
Generate the ABI specification file [EXPERIMENTAL]
以上是一个合约的基本步骤。但在合约编译中遇到很多问题,下面对编译过程中的问题进行汇总
问题一:生成的基本结构编译报错,报错内容如下:
[lz@localhost mytest]$ eosiocpp -o mytest mytest.cpp
mytest.cpp:15:39: error: no matching conversion for functional-style cast from 'uint64_t' (aka 'unsigned long long') to 'eosio::name'
eosio::print( "Hello World: ", eosio::name(code), "->", eosio::name(action), "\n" );
^~~~~~~~~~~~~~~~
/data/blockchain/eos/contracts/eosiolib/types.hpp:75:11: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'uint64_t' (aka 'unsigned long long') to 'const eosio::name' for 1st argument
struct name {
^
/data/blockchain/eos/contracts/eosiolib/types.hpp:75:11: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'uint64_t' (aka 'unsigned long long') to 'eosio::name' for 1st argument
/data/blockchain/eos/contracts/eosiolib/types.hpp:75:11: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
mytest.cpp:15:64: error: no matching conversion for functional-style cast from 'uint64_t' (aka 'unsigned long long') to 'eosio::name'
eosio::print( "Hello World: ", eosio::name(code), "->", eosio::name(action), "\n" );
^~~~~~~~~~~~~~~~~~
/data/blockchain/eos/contracts/eosiolib/types.hpp:75:11: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'uint64_t' (aka 'unsigned long long') to 'const eosio::name' for 1st argument
struct name {
^
/data/blockchain/eos/contracts/eosiolib/types.hpp:75:11: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'uint64_t' (aka 'unsigned long long') to 'eosio::name' for 1st argument
/data/blockchain/eos/contracts/eosiolib/types.hpp:75:11: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
2 errors generated.
本错误是生成的结构中有一个强制类型转换的问题,原文件内容如下:
extern "C" ä
/// The apply method implements the dispatch of events to this contract
void apply( uint64_t receiver, uint64_t code, uint64_t action ) ä
eosio::print( "Hello World: ", eosio::name(code), "->", eosio::name(action), "ön" );
å
å // extern "C"
根据github上修改文件,参考https://github.com/EOSIO/eos/issues/2061,修改后内容:
extern "C" ä
/// The apply method implements the dispatch of events to this contract
void apply( uint64_t receiver, uint64_t code, uint64_t action ) ä
// eosio::print( "Hello World: ", eosio::name(code), "->", eosio::name(action), "ön" );
eosio::name codeName;
codeName.value = code;
eosio::name actionName;
actionName.value = action;
eosio::print("Hello world:",codeName,"->",actionName,"ön");
å
å // extern "C"
问题二、编译中出现缺少文件,libc.bc,libc++.bc,eosiolib.bc,eosio-s2wasm文件,报错内容如下:
Älz@localhost mytestÅ$ eosiocpp -o mytest.wast mytest.cpp
/home/lz/opt/wasm/bin/llvm-link: /data/blockchain/eos/build/usr/share/eosio/contractsdk/lib/libc.bc: error: Could not open input file: No such file or directory
/home/lz/opt/wasm/bin/llvm-link: error loading file '/data/blockchain/eos/build/usr/share/eosio/contractsdk/lib/libc.bc'
Älz@localhost mytestÅ$ eosiocpp -o mytest.wast mytest.cpp
/home/lz/opt/wasm/bin/llvm-link: /data/blockchain/eos/build/usr/share/eosio/contractsdk/lib/libc++.bc: error: Could not open input file: No such file or directory
/home/lz/opt/wasm/bin/llvm-link: error loading file '/data/blockchain/eos/build/usr/share/eosio/contractsdk/lib/libc++.bc'
Älz@localhost mytestÅ$ eosiocpp -o mytest.wast mytest.cpp
/home/lz/opt/wasm/bin/llvm-link: /data/blockchain/eos/build/usr/share/eosio/contractsdk/lib/eosiolib.bc: error: Could not open input file: No such file or directory
/home/lz/opt/wasm/bin/llvm-link: error loading file '/data/blockchain/eos/build/usr/share/eosio/contractsdk/lib/eosiolib.bc'
Älz@localhost mytestÅ$ eosiocpp -o mytest.wast mytest.cpp
/data/blockchain/eos/build/tools/eosiocpp:行60: /data/blockchain/eos/build/bin/eosio-s2wasm: 没有那个文件或目录
原因是在tools/eosiocpp.in文件中的路径设置与eos环境编译后的路径不一致,eosiocpp.in文件配置路径如下:
($PRINT_CMDS; @WASM_LLVM_LINK@ -only-needed -o $workdir/linked.bc $workdir/built/* ö
$äEOSIO_INSTALL_DIRå/usr/share/eosio/contractsdk/lib/libc.bc ö
$äEOSIO_INSTALL_DIRå/usr/share/eosio/contractsdk/lib/libc++.bc ö
$äEOSIO_INSTALL_DIRå/usr/share/eosio/contractsdk/lib/eosiolib.bc
)
($PRINT_CMDS; @WASM_LLC@ -thread-model=single --asm-verbose=false -o $workdir/assembly.s $workdir/linked.bc)
($PRINT_CMDS; $äEOSIO_INSTALL_DIRå/bin/eosio-s2wasm -o $outname -s 16384 $workdir/assembly.s)
解决办法是找到编译后的位置,然后设置软链接
find / -name libc.bc
ln -s /data/blockchain/eos/build/contracts/musl/libc.bc libc.bc
find / -name libc++.bc
ln -s /data/blockchain/eos/build/contracts/libc++/libc++.bc libc++.bc
find / -name eosiolib.bc
ln -s /data/blockchain/eos/build/contracts/eosiolib/eosiolib.bc eosiolib.bc
find / -name eosio-s2wasm
ln -s /data/blockchain/eos/build/externals/binaryen/bin/eosio-s2wasm eosio-s2wasm
设置后即可通过编译。
Älz@localhost mytestÅ$ eosiocpp -o mytest.wast mytest.cpp
Älz@localhost mytestÅ$ ls
mytest.abi mytest.cpp mytest.hpp mytest.wast
问题三、在生成abi文件时报缺少eosio-abigen
lz@localhost mytestÅ$ eosiocpp -g mytest.abi mytest.cpp
/data/blockchain/eos/build/tools/eosiocpp:行76: /data/blockchain/eos/build/bin/eosio-abigen: 没有那个文件或目录
解决办法同上,通过设置软链接解决
ln -s /data/blockchain/eos/build/programs/eosio-abigen/eosio-abigen eosio-abigen
设置即可成功生成
Älz@localhost mytestÅ$ eosiocpp -g mytest.abi mytest.cpp
1831918ms thread-0 abi_generator.hpp:68 ricardian_contracts Å Warning, no ricardian clauses found for
Generated mytest.abi ...