hera工程在linux下比较好编译,但是在windows下如果使用cmake生成vc的工程编译就各种错误,因此采用cygwin来编译
1、去除cmake,make的环境变量
由于开始为了在vc下编译,分别下载了cmake-3.13.3-win64-x64.msi 和 gnuwin32 去除path里面的: C:Program FilesCMakebin C:Program Files (x86)GnuWin32bin 因为这些如果存在,cmake出来的一定还是vc版本
2、安装cygwin
参考文章 如何在Windows中编译Linux Unix的代码(采用cygwin) 这里有几个错误,
2.1、设置163镜像的地址不对,应该是:http://mirrors.163.com/cygwin/
2.2、库名称有点问题
Devel 选项下的gcc-core,gcc-g ,make 以及Lib选项卡下的libglib2,libglib2-devel
2.3 另外需要安装的库
增加安装cmake
3、下载编译hera
启动cygwin,执行
代码语言:javascript复制cd /cygdrive/d/temp/
git clone https://github.com/ewasm/hera
cd hera
git submodule update --init
mkdir build
cd build
cmake ..
make
如果出现下面的错误:
代码语言:javascript复制CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
CMake 3.8 or higher is required. You are running version 3.6.2
需要升级cygwin的cmake版本,否则后续代码编译会出错 编译成功后会在buildsrc生成文件cyghera.dll
4、调用测试
4.1、修改heraevmcexamplesCMakeLists.txt
内容为:
代码语言:javascript复制# EVMC: Ethereum Client-VM Connector API.
# Copyright 2018 The EVMC Authors.
# Licensed under the Apache License, Version 2.0. See the LICENSE file.
cmake_minimum_required(VERSION 3.8)
include(GNUInstallDirs)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()
set(CMAKE_DEBUG_POSTFIX "")
include_directories(../include
../../include
)
add_library(evmc-example-host STATIC example_host.cpp)
target_link_libraries(evmc-example-host PRIVATE)
add_library(evmc-example-vm example_vm.c)
target_link_libraries(evmc-example-vm PRIVATE)
set_source_files_properties(example_vm.c PROPERTIES COMPILE_DEFINITIONS PROJECT_VERSION=${PROJECT_VERSION})
#add_executable(evmc-example example.c)
#target_link_libraries(evmc-example PRIVATE evmc-example-host evmc-example-vm)
add_executable(evmc-example example.c loader.c)
target_link_libraries(evmc-example PRIVATE evmc-example-host evmc-example-vm)
install(TARGETS evmc-example-vm
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
4.2、复制loader.c
代码语言:javascript复制cd /cygdrive/d/temp/hera/evmc/examples
cp ../lib/loader/loader.c .
4.3、修改example.c
代码语言:javascript复制/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2018 The EVMC Authors.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
*/
#include "example_host.h"
#include "example_vm.h"
#include <evmc/helpers.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <evmc/loader.h>
int main()
{
enum evmc_loader_error_code ec;
evmc_create_fn create_fn = evmc_load("libhera.dll", &ec);
if(ec != EVMC_LOADER_SUCCESS)
{
printf("load libhera.dll error,err=%d",ec);
}
struct evmc_instance* vm = create_fn();
if (!evmc_is_abi_compatible(vm))
return 1;
// EVM bytecode goes here. This is one of the examples.
const uint8_t code[] = "x30x60x00x52x59x60x00xf3";
const size_t code_size = strlen((char*)code);
const uint8_t input[] = "Hello World!";
const evmc_uint256be value = {{1, 0}};
const evmc_address addr = {{0, 1, 2}};
const int64_t gas = 200000;
struct evmc_context* ctx = example_host_create_context();
struct evmc_message msg;
msg.sender = addr;
msg.destination = addr;
msg.value = value;
msg.input_data = input;
msg.input_size = sizeof(input);
msg.gas = gas;
msg.depth = 0;
struct evmc_result result = evmc_execute(vm, ctx, EVMC_HOMESTEAD, &msg, code, code_size);
printf("Execution result:n");
if (result.status_code != EVMC_SUCCESS)
{
printf(" EVM execution failure: %dn", result.status_code);
}
else
{
printf(" Gas used: %" PRId64 "n", gas - result.gas_left);
printf(" Gas left: %" PRId64 "n", result.gas_left);
printf(" Output size: %zdn", result.output_size);
printf(" Output: ");
size_t i = 0;
for (i = 0; i < result.output_size; i )
printf("x ", result.output_data[i]);
printf("n");
}
evmc_release_result(&result);
example_host_destroy_context(ctx);
evmc_destroy(vm);
return 0;
}
4.4、编译
代码语言:javascript复制mkdir build
cd build
cmake ..
make
4.5、复制cyghera.dll,并改名
代码语言:javascript复制cp ../../../build/src/cyghera.dll libhera.dll
4.6、执行
代码语言:javascript复制 ./evmc-example.exe
Executing message in Hera
InternalError: Only Byzantium supported.
Execution result:
EVM execution failure: -1
Segmentation fault (核心已转储)
目前存在的问题: java调用这个dll一定crash,依赖的dll都能够找到,但是加载失败