eos合約開發流程以及編譯中遇到的問題

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 ...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章