html中的文档格式及举例,跟我一起从零开始学习WebAssembly(三)、最简单的例子hello world(使用自定义HTML模板)...
文章目錄
創建C++代碼片
創建我們的自定義HTML模板文件
編譯
運行實例
有時我們想要使用我們自定義HTML模板。讓我們來看看我們如何做到這一點。
創建C++代碼片
首先,創建一個名為hello2的目錄。
其次,在該目錄下創建一個名為hello2.c文件。
并將以下C++代碼保存在文件中:
#include
int main(int argc, char ** argv)
{
printf("Hello World\n");
}
創建我們的自定義HTML模板文件
1、在我們的emsdk庫中搜索shell_minimal.html文件(如我的是:D:\emsdk\fastcomp\emscripten\src)。
2、在我們當前目錄的上一級創建一個html_template目錄。
3、將shell_minimal.html文件復制到html_template目錄。
編譯
現在進入到hello2目錄(再次,在Emscripten編譯器環境終端窗口中),然后運行以下命令:
emcc -o hello2.html hello2.c -O3 -s WASM=1 --shell-file html_template/shell_minimal.html
然而我們發現會報錯,
Traceback (most recent call last):
File "D:\emsdk\fastcomp\emscripten\emcc.py", line 3420, in
sys.exit(run(sys.argv))
File "D:\emsdk\fastcomp\emscripten\emcc.py", line 2318, in run
memfile, optimizer)
File "D:\emsdk\fastcomp\emscripten\emcc.py", line 3237, in generate_html
wasm_binary_target, memfile, optimizer)
File "D:\emsdk\fastcomp\emscripten\emcc.py", line 3010, in generate_traditional_runtime_html
shell = read_and_preprocess(options.shell_path)
File "D:\emsdk\fastcomp\emscripten\tools\shared.py", line 3272, in read_and_preprocess
run_js(path_from_root('tools/preprocessor.js'), NODE_JS, args, True, stdout=open(stdout, 'w'), cwd=path)
File "D:\emsdk\fastcomp\emscripten\tools\shared.py", line 1089, in run_js
return jsrun.run_js(filename, engine, *args, **kw)
File "D:\emsdk\fastcomp\emscripten\tools\jsrun.py", line 129, in run_js
universal_newlines=True)
File "D:\emsdk\python\2.7.13.1_64bit\python-2.7.13.amd64\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "D:\emsdk\python\2.7.13.1_64bit\python-2.7.13.amd64\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 267]
如圖:
根據提示,我們跟蹤到最后一個文件,看看里面的內容:
我們主要看拋異常的地方:
except pywintypes.error, e:
# Translate pywintypes.error to WindowsError, which is
# a subclass of OSError. FIXME: We should really
# translate errno using _sys_errlist (or similar), but
# how can this be done from Python?
raise WindowsError(*e.args)
看看官方的描述:將pywintypes.error轉換為WindowsError,它是OSError的子類。 FIXME:我們應該使用_sys_errlist(或類似的)來轉換錯誤碼,但是如何從Python中完成呢?。
那么說白了就是將python的錯誤碼轉換為window錯誤碼。
最終輸出:
WindowsError: [Error 267],其實就給我們windows中使用的GetlastError一樣,而我們找到windows錯誤碼為267,是路勁錯誤。
最終定位到我們命令中的html_template/shell_minimal.html路勁有問題,要么使用絕對路勁,要么前面加上../,
最后我們修改如下:
emcc -o hello2.html hello2.c -O3 -s WASM=1 --shell-file ../html_template/shell_minimal.html
編譯成功。
我們通過的選項這次略有不同:
我們已經指定了-o hello2.html,這意味著編譯器仍將輸出JavaScript粘合代碼和.html。
我們還指定了–shell-file html_template/shell_minimal.html- 這提供了您想要用來創建HTML的HTML模板的路徑,您將通過該示例運行。
運行實例
現在讓我們運行這個例子。上面的命令將生成hello2.html,它將與模板具有相同的內容,并添加一些粘合代碼以加載生成的wasm,運行它等。在瀏覽器中打開它,你會看到與最后一個相同的輸出例。
先看看我們的模板效果:
再看實例效果,看看是否跟模板一樣:
注意:您可以通過在-o標志中指定.js文件而不是HTML文件來指定僅輸出JavaScript“glue”文件*而不是完整的HTML ,例如 emcc -o hello2.js hello2.c -O3 -s WASM=1。然后,您可以從頭開始構建自定義HTML,盡管這是一種高級方法; 使用提供的HTML模板通常更容易。
Emscripten需要各種各樣的JavaScript“粘合”代碼來處理內存分配,內存泄漏以及許多其他問題。
總結
以上是生活随笔為你收集整理的html中的文档格式及举例,跟我一起从零开始学习WebAssembly(三)、最简单的例子hello world(使用自定义HTML模板)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 请尽快升级 WinRAR 至 6.23
- 下一篇: “星芒”效果是怎么实现的?怎么拍 &qu