所以我想将一个简单的c ++文件编译成js并在网络浏览器中运行。我采取的步骤(在Windows上):
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
emsdk install latest
emsdk activate latest
emsdk_env.bat --global
emsdk update-tags
emcc -v
最后打印的命令:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.39.15
clang version 11.0.0 "
然后我试图在main.cpp文件中编译如下所示的“ hello world”:
#include <iostream>
#include <emscripten.h>
int main()
{
std::cout<< "hello world!" << std::endl;
return 0;
}
然后在测试文件夹中,我使用了命令:
em++ main.cpp -o 1.html
其中印刷:
cache:INFO: generating system library: libcompiler_rt.a... (this will be cached in "C:\Users\x\.emscripten_cache\wasm\libcompiler_rt.a" for subsequent builds)
cache:INFO: - ok
cache:INFO: generating system library: libc-wasm.a... (this will be cached in "C:\Users\x\.emscripten_cache\wasm\libc-wasm.a" for subsequent builds)
cache:INFO: - ok
cache:INFO: generating system library: libc++-noexcept.a... (this will be cached in "C:\Users\x\.emscripten_cache\wasm\libc++-noexcept.a" for subsequent builds)
cache:INFO: - ok
cache:INFO: generating system library: libc++abi-noexcept.a... (this will be cached in "C:\Users\x\.emscripten_cache\wasm\libc++abi-noexcept.a" for subsequent builds)
cache:INFO: - ok
cache:INFO: generating system library: libdlmalloc.a... (this will be cached in "C:\Users\x\.emscripten_cache\wasm\libdlmalloc.a" for subsequent builds)
cache:INFO: - ok
cache:INFO: generating system library: libpthread_stub.a... (this will be cached in "C:\Users\x\.emscripten_cache\wasm\libpthread_stub.a" for subsequent builds)
cache:INFO: - ok
cache:INFO: generating system library: libc_rt_wasm.a... (this will be cached in "C:\Users\x\.emscripten_cache\wasm\libc_rt_wasm.a" for subsequent builds)
cache:INFO: - ok
cache:INFO: generating system library: libsockets.a... (this will be cached in "C:\Users\x\.emscripten_cache\wasm\libsockets.a" for subsequent builds)
cache:INFO: - ok
作为输出,我得到了以下文件:1.html
,1.js
,1.wasm
当我打开1.html
时,我看到这些错误:
both async and sync fetching of the wasm failed
RuntimeError: abort(both async and sync fetching of the wasm failed) at jsStackTrace@file:///E:/emscripten/emsdk/test/1.js:1952:17
stackTrace@file:///E:/emscripten/emsdk/test/1.js:1969:16
abort@file:///E:/emscripten/emsdk/test/1.js:1728:44
getBinary@file:///E:/emscripten/emsdk/test/1.js:1800:10
getBinaryPromise/<@file:///E:/emscripten/emsdk/test/1.js:1822:13
getBinaryPromise@file:///E:/emscripten/emsdk/test/1.js:1821:10
instantiateArrayBuffer@file:///E:/emscripten/emsdk/test/1.js:1865:12
instantiateAsync@file:///E:/emscripten/emsdk/test/1.js:1892:14
createWasm@file:///E:/emscripten/emsdk/test/1.js:1908:3
@file:///E:/emscripten/emsdk/test/1.js:4990:11
我也在Linux上尝试过,但是出现了完全相同的错误。 emscripten SDK的旧版本也不起作用。
任何提示?
使用file://
的问题,我认为您需要运行服务器并使用http://
。参见:https://emscripten.org/docs/getting_started/FAQ.html#faq-local-webserver。并且:https://emscripten.org/docs/compiling/Running-html-files-with-emrun.html