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