带Protobuf的C++程序编译为WASM后无法用Wasmtime运行的问题咨询
问题:Emscripten编译Protobuf C++程序为WASM后无法在Wasmtime运行及转换为Wat格式
我尝试用Emscripten将基于Protobuf的C++程序编译为WASM,但编译成功后,既无法用Wasmtime运行,也无法用wasm2wat转换为.wat格式。
具体错误信息
1. Wasmtime运行报错
# wasmtime person.wasm Error: failed to run main module `person.wasm` Caused by: 0: if you're trying to run a precompiled module, pass --allow-precompiled 1: failed to compile wasm function 79 at offset 0x293c 2: WebAssembly translation error 3: Invalid input WebAssembly code at offset 10632: threads support is not enabled
2. wasm2wat转换报错
# wasm2wat person.wasm 000298a: error: unexpected opcode: 0xfe 0x10
文件目录结构
protobuf_wasm_test/ -lib/ -libprotobuf-lite.a -libprotobuf.a -libprotoc.a -pb/ -person.pb.cc -person.pb.h -CMakeLists.txt -person.cpp
相关文件内容
pb/person.proto
syntax = "proto3"; message Person { string id = 1; string name = 2; }
person.cpp
#include <iostream> #include "./pb/person.pb.h" int main() { Person *person = (Person*)malloc(sizeof(Person)); person->set_id("12345"); person->set_name("abc"); std::cout<<"id: "<<person->id()<<", name: "<<person->name()<<std::endl; free(person); return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.5) project(protobuf_wasm_test) add_compile_options(-sSTANDALONE_WASM -Wno-unused-command-line-argument) include_directories("${CMAKE_CURRENT_BINARY_DIR}/../pb") include_directories("/home/hzb/protobuf_wasm/protobuf_src/src") link_directories("${CMAKE_CURRENT_BINARY_DIR}/../lib") set(proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/../pb/person.pb.cc") set(proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/../pb/person.pb.h") add_library(proto ${proto_srcs} ${proto_hdrs}) add_executable(person person.cpp) target_link_libraries(person proto protobuf)
编译过程
我已用Emscripten重新编译Protobuf源码,生成libprotobuf.a存于./lib/目录,编译命令及日志如下:
编译命令
emcmake .. emmake make
编译日志
configure: cmake .. -DCMAKE_TOOLCHAIN_FILE=/home/hzb/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR=/home/hzb/emsdk/node/14.18.2_64bit/bin/node;--experimental-wasm-bulk-memory;--experimental-wasm-threads -- Configuring done -- Generating done -- Build files have been written to: /home/hzb/protobuf_wasm/protobuf_wasm_test/build make: make [ 25%] Building CXX object CMakeFiles/proto.dir/pb/person.pb.cc.o [ 50%] Linking CXX static library libproto.a [ 50%] Built target proto [ 75%] Building CXX object CMakeFiles/person.dir/person.cpp.o [100%] Linking CXX executable person.js [100%] Built target person
额外疑问
如何将包含第三方库的复杂C++程序编译为WASM并在Wasmtime等非Web WASM运行时运行?将每个库编译为WASM静态库(libxxx.a)后再链接到目标程序是否可行?
内容的提问来源于stack exchange,提问作者Han
相关产品推荐
相关产品推荐

