开发工具 EOS 智能合约快速开发--全自动 IDE 配置 (CLion)

liweidong · May 16, 2018 · Last by liweidong replied at May 18, 2018 · 13951 hits
自动IDE配置(CLion)--可实现自动提示,语法检查,方法跳转,编译生成wast,abi文件等功能。
CMake开发只需要编写CMakeLists.txt文件即可夸平台使用。
eos智能合约官方编译使用eosiocpp脚本,因此根据eosiocpp里的内容编写即可,以下是基于mac版eos配置内容。linux环境的一些安装路径需要自行修改。
使用方式:把以下两个文件放到同一个目录,使用CLion导入项目即可。
最终效果如图:


1、文件 CMakeLists.txt

make_minimum_required(VERSION 3.9)
project(templet CXX)


set(EOSIO_INSTALL_DIR /usr/local)
set(BOOST_INCLUDE_DIR /usr/local/include)
set(WASM_INSTALL_DIR /usr/local/wasm)
set(S2WASM_PATH ${EOSIO_INSTALL_DIR}/bin/eosio-s2wasm)
set(WASTS2WASM_PATH ${EOSIO_INSTALL_DIR}/bin/eosio-wast2wasm)
set(ABIGEN ${EOSIO_INSTALL_DIR}/bin/eosio-abigen)
set(EOSIO_CONTRACTSDK_PATH ${EOSIO_INSTALL_DIR}/usr/share/eosio/contractsdk)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -emit-llvm")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++14")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --target=wasm32")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdinc")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlibinc")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffreestanding")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-threadsafe-statics")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${EOSIO_INSTALL_DIR}/include
    ${EOSIO_INSTALL_DIR}/include/libc++/upstream/include
    ${EOSIO_INSTALL_DIR}/include/musl/upstream/include
    ${BOOST_INCLUDE_DIR}
)


file(GLOB_RECURSE RESOURCE_FILES  ${CMAKE_CURRENT_SOURCE_DIR} *.c *.h *.cpp *.hpp )

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_CXX_COMPILER
        ${WASM_INSTALL_DIR}/bin/clang)
set(CMAKE_CXX_CREATE_SHARED_LIBRARY
        "${WASM_INSTALL_DIR}/bin/llvm-link -only-needed -o <TARGET> <OBJECTS>")

add_library(templet SHARED
        ${RESOURCE_FILES})

target_link_libraries(templet
        ${EOSIO_CONTRACTSDK_PATH}/lib/libc.bc
        ${EOSIO_CONTRACTSDK_PATH}/lib/libc++.bc
        ${EOSIO_CONTRACTSDK_PATH}/lib/eosiolib.bc)

set(CONTRACT_ASSEMBLY_FILE ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.s)
set(CONTRACT_WAST_FILE_T ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}_t.wast)
set(CONTRACT_WAST_FILE ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.wast)
set(CONTRACT_ABI_FILE ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.abi)

add_custom_target(assembly ALL
        DEPENDS templet)
add_custom_command(
        TARGET assembly
#        OUTPUT ${CONTRACT_ASSEMBLY_FILE}
        COMMAND ${WASM_INSTALL_DIR}/bin/llc
        -thread-model=single
        --asm-verbose=false
        -o ${CONTRACT_ASSEMBLY_FILE} $<TARGET_FILE:templet>
        DEPENDS $<TARGET_FILE:templet>)


add_custom_target(wast ALL
        DEPENDS assembly)
add_custom_command(
        TARGET wast
#        OUTPUT ${CONTRACT_WAST_FILE_T}
        COMMAND ${S2WASM_PATH}
        -o ${CONTRACT_WAST_FILE_T}
        -s 16384
        ${CONTRACT_ASSEMBLY_FILE}
        DEPENDS ${CONTRACT_ASSEMBLY_FILE})


add_custom_target(wast2 ALL
        DEPENDS wast)
add_custom_command(
        TARGET wast2
#        OUTPUT ${CONTRACT_WAST_FILE}
        COMMAND ${WASTS2WASM_PATH}
        ${CONTRACT_WAST_FILE_T}
        ${CONTRACT_WAST_FILE}
        -n
        DEPENDS ${CONTRACT_WAST_FILE_T})

add_custom_target(abi ALL)
add_custom_command(
        TARGET abi
        COMMAND ${ABIGEN} -extra-arg=-c
        -extra-arg=--std=c++14
        -extra-arg=--target=wasm32
        -extra-arg=-nostdinc
        -extra-arg=-nostdinc++
        -extra-arg=-DABIGEN
        -extra-arg=-I${EOSIO_INSTALL_DIR}/include/libc++/upstream/include
        -extra-arg=-I${EOSIO_INSTALL_DIR}/include/musl/upstream/include
        -extra-arg=-I${BOOST_INCLUDE_DIR}
        -extra-arg=-I${EOSIO_INSTALL_DIR}/include
        -extra-arg=-I${CMAKE_CURRENT_SOURCE_DIR}
        -extra-arg=-fparse-all-comments -destination-file=${CONTRACT_ABI_FILE} -verbose=0
        -context=${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/interface.cpp --
)

2、文件 interface.cpp

#include <vector>
#include <eosiolib/print.hpp>
#include <eosiolib/contract.hpp>
#include <eosiolib/dispatcher.hpp>

using namespace eosio;

class interface:public contract
{
public:
    interface(account_name accout):contract(accout){

    }
    // @abi action
    void hi(account_name user ) {
//    require_auth( user );
        eosio::print( "hi, ", name{user});
    }
    // @abi action
    void abinum (uint64_t num) {
        eosio::print( "abinum: ",num );
    }

};

EOSIO_ABI(interface, (hi)(abinum))

这个是和之前一样的吗 、

之前的文档还可以再次编辑的。。

之前那个账号注册错了,邮箱写错了。直接把账号删除了,文章帮忙删一下。

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up