hera工程在linux下比較好編譯,但是在windows下如果使用cmake生成vc的工程編譯就各種錯誤,因此采用cygwin來編譯
1、去除cmake,make的環(huán)境變量
由于開始為了在vc下編譯,分別下載了cmake-3.13.3-win64-x64.msi 和 gnuwin32
去除path里面的:
C:\Program Files\CMake\bin
C:\Program Files (x86)\GnuWin32\bin
因為這些如果存在,cmake出來的一定還是vc版本
2、安裝cygwin
參考文章 如何在Windows中編譯Linux Unix的代碼(采用cygwin)
這里有幾個錯誤,
2.1、設(shè)置163鏡像的地址不對,應(yīng)該是:http://mirrors.163.com/cygwin/
2.2、庫名稱有點(diǎn)問題
Devel 選項下的gcc-core,gcc-g++,make 以及Lib選項卡下的libglib2,libglib2-devel
2.3 另外需要安裝的庫
增加安裝cmake
3、下載編譯hera
啟動cygwin,執(zhí)行
cd /cygdrive/d/temp/
git clone https://github.com/ewasm/hera
cd hera
git submodule update --init
mkdir build
cd build
cmake ..
make
如果出現(xiàn)下面的錯誤:
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版本,否則后續(xù)代碼編譯會出錯
編譯成功后會在build\src\生成文件cyghera.dll
4、調(diào)用測試
4.1、修改hera\evmc\examples\CMakeLists.txt
內(nèi)容為:
# 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、復(fù)制loader.c
cd /cygdrive/d/temp/hera/evmc/examples
cp ../lib/loader/loader.c .
4.3、修改example.c
/* 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[] = "\x30\x60\x00\x52\x59\x60\x00\xf3";
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: %d\n", result.status_code);
}
else
{
printf(" Gas used: %" PRId64 "\n", gas - result.gas_left);
printf(" Gas left: %" PRId64 "\n", result.gas_left);
printf(" Output size: %zd\n", result.output_size);
printf(" Output: ");
size_t i = 0;
for (i = 0; i < result.output_size; i++)
printf("%02x ", result.output_data[i]);
printf("\n");
}
evmc_release_result(&result);
example_host_destroy_context(ctx);
evmc_destroy(vm);
return 0;
}
4.4、編譯
mkdir build
cd build
cmake ..
make
4.5、復(fù)制cyghera.dll,并改名
cp ../../../build/src/cyghera.dll libhera.dll
4.6、執(zhí)行
./evmc-example.exe
Executing message in Hera
InternalError: Only Byzantium supported.
Execution result:
EVM execution failure: -1
Segmentation fault (核心已轉(zhuǎn)儲)
目前存在的問題:
java調(diào)用這個dll一定crash,依賴的dll都能夠找到,但是加載失敗