web assembly能做什么?
WebAssembly 或者 wasm 是一個可移植、體積小、加載快并且兼容 Web 的全新格式。
常見的大型項目中,AutoCAD的web版本,谷歌地球的web版本,都是使用web assembly實現的。
這玩意可以把現有的c++ c等程序源碼編譯成可供瀏覽器讀取的格式,而且執(zhí)行效率還奇快。
具體請參考https://www.cnblogs.com/detectiveHLH/p/9928915.html
安裝web assembly
1、安裝git bash https://gitforwindows.org/ 進去后點擊download即可,這里大家都會我就不多說了。
2、下載安裝python 2.7 https://www.python.org/downloads/windows/
進去后下載 下圖所示內容

記得在安裝程序中勾選“添加環(huán)境變量”,默認這個選項是不被勾選的。
3、下載安裝node 需要12.9.x及以上版本http://nodejs.cn/download/
4、下載安裝cmake https://cmake.org/download/

5、下載安裝visual studio 千萬看清,是vs,不是vs code一個是IDE,一個是編輯器。
https://visualstudio.microsoft.com/zh-hans/vs/
安裝時選擇和c++有關的選項即可,大概如下圖:

6、下載web assembly
新建目錄"wa",然后參考下方命令
cd wa
git clone https://github.com/juj/emsdk.git
cd emsdk
// 下方命令根據系統(tǒng)選擇
// Mac
$ ./emsdk install latest
$ ./emsdk activate latest
// Windows
emsdk install latest
emsdk activate latest
下一步,繼續(xù)輸入命令
// Mac
$ source ./emsdk_env.sh
// Windows
emsdk_env
執(zhí)行上面一系列命令時,請保證良好的網絡環(huán)境,否則可能要一天。
windows10系統(tǒng),無比在管理員權限下執(zhí)行命令行,否則運行啥都會無效。
上面命令如果按照官網示例來輸入,則會一個找不到SDK的錯,反正我是這樣的。
寫個示例程序(來自官網示例)
執(zhí)行下方命令
$ mkdir hello
$ cd hello
$ echo '#include <stdio.h>' > hello.c
$ echo 'int main(int argc, char ** argv) {' >> hello.c
$ echo 'printf("Hello, world!\n");' >> hello.c
$ echo '}' >> hello.c
$ emcc hello.c -s WASM=1 -o hello.html
這個程序可以自己用vs來寫,就是一個簡單的hello world,保存為hello.c文件,并執(zhí)行
emcc hello.c -s WASM=1 -o hello.html
這個命令來編譯成hello.html
運行試試
$ emrun --no_browser --port 8080 .
打開localhost:8080 看一下效果吧。
編譯C++
a.cpp
#include <iostream>
using namespace std;
int fn() {
char asd[] = "asd";
cout << asd << endl;
return 0;
}
int main()
{
cout << "Hello, world!" << endl;
fn();
return 0;
}
編譯
emcc a.cpp -s WASM=1 -o hello.html
執(zhí)行
emrun --no_browser --port 8080 .
效果
