用于将C++代码转换为 Web 程序集的脚本未终止



我想将CPP文件编译为Web程序集。我在以下链接中使用脚本

https://github.com/wasdk/wasmexplorer-service/tree/master/scripts

我在我的 Linux 操作系统中使用以下命令

../scripts/compile2.sh ../test.cpp "-fno-verbose-asm -03 -std=c++98" 2>&1

命令永远不会终止。我已经尝试了所有选项组合。谁能在这方面指导我?或任何其他将C++代码转换为相应 Web 汇编代码的方法(帮助我设计一个独立的应用程序(。

文件内容如下:

double fact(int i) {
long long n = 1;
for (;i > 0; i--) {
n *= i;
}
return (double)n;
}

我的预期输出如下(来自 https://mbebenita.github.io/WasmExplorer/(

(module
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(export "_Z4facti" (func $_Z4facti))
(func $_Z4facti (; 0 ;) (param $0 i32) (result f64)
(local $1 i64)
(local $2 i64)
(block $label$0
(br_if $label$0
(i32.lt_s
(get_local $0)
(i32.const 1)
)
)
(set_local $1
(i64.add
(i64.extend_s/i32
(get_local $0)
)
(i64.const 1)
)
)
(set_local $2
(i64.const 1)
)
(loop $label$1
(set_local $2
(i64.mul
(get_local $2)
(tee_local $1
(i64.add
(get_local $1)
(i64.const -1)
)
)
)
)
(br_if $label$1
(i64.gt_s
(get_local $1)
(i64.const 1)
)
)
)
(return
(f64.convert_s/i64
(get_local $2)
)
)
)
(f64.const 1)
)
)

运行以下代码对我有用

/*
# run me with 'bash test.cpp'
clang++ $0 --target=wasm32-unknown-unknown-wasm -nostartfiles -nostdlib -Wl,--no-entry -Wl,--export-all -o test.wasm
exit 1
*/
extern "C" double fact(int i) {
long long n = 1;
for (;i > 0; i--) {
n *= i;
}
return (double)n;
}

测试

<!DOCTYPE html>
<script type="module">
async function init() {
const { instance } = await WebAssembly.instantiateStreaming(fetch("./test.wasm"));
console.log(instance.exports.fact(6));
}
init();
</script>

相关内容

  • 没有找到相关文章

最新更新