1. Debian 10.6中安裝Emscripten安裝:
參考:https://emscripten.org/docs/getting_started/downloads.html
git clone https://github.com/juj/emsdk.git
cd emsdk
./emsdk update
git pull
./emsdk install latest
在./emsdk-2.0.8/.emscripten的emsdk_path路勁後添加:
LLVM_ROOT = emsdk_path + '/upstream/bin'
在/etc/profile最後添加如下,使環境變量PATH添加clang和emscripten路徑:
export PATH=/root/emsdk-2.0.8/upstream/emscripten:/root/emsdk-2.0.8/upstream/bin:$PATH
保存後,執行如下命令使其生效:
source /etc/profile
cd emsdk
./emsdk activate latest
查看版本
emcc --version
em++ --version
./emsdk list
安裝binaryen:
./emsdk install binaryen-tag-1.38.31-64bit
./emsdk activate binaryen-tag-1.38.31-64bit
source ./emsdk_env.sh
./emsdk list
安裝web server服務器nginx:
apt install nginx
打開nginx config目錄下的mime.types文件,在types中添加
application/wasm wasm;
編寫hello.c文件:
#include <stdio.h>
int main(int argc, char ** argv) {
printf("Hello World\n");
}
emcc編譯
emcc hello.c -s WASM=1 -O3 -o hello.html
編寫hello.cpp文件:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!"<<endl;
return 1;
}
emcc/em++將c++代碼轉化爲js代碼將c代碼轉化爲js代碼:
em++ hello.cpp -s WASM=1 -O3 -o hello.html
emcc hello.cpp -s WASM=1 -O3 -o hello.html
注意:
C++代碼轉換js的cout最後要有換行
使用一個支持 WebAssembly 的瀏覽器,加載生成的 hello.html。
提示:Firefox 52+和 Chrome 57+和最新版本的Opera已經默認啓用,也可以在 Firefox 47+ 中通過在 about:config 頁面啓用 javascript.options.wasm 字段獲得支持,Chrome 51+和 Opera 38+可以在 chrome://flags 頁面啓用 Experimental WebAssembly 選項以支持 WebAssembly。
瀏覽器中只能在URL以http或者https開頭,不能用file開頭:
有時候編譯會出現內存不夠,需要修改emsdk目錄中的文件upstream/emscripten/src/settings.js
// var INITIAL_MEMORY = 16777216;
var INITIAL_MEMORY = 33554432; // 32M