最近一直在捣鼓webassembly 的东西,趁着还有个印象,赶紧记下来。

老规矩,上官网,https://emscripten.org/,按照官网教程一步步来(强烈建议挂代理)。

git clone https://github.com/emscripten-core/emsdk.git

# Enter that directory
cd emsdk

Fetch the latest version of the emsdk (not needed the first time you clone)
git pull

# Download and install the latest SDK tools.
./emsdk install latest

# Make the "latest" SDK "active" for the current user. (writes .emscripten file)
./emsdk activate latest

# Activate PATH and other environment variables in the current terminal
source ./emsdk_env.sh

On Windows, run emsdk instead of ./emsdk, and emsdk_env.bat instead of source ./emsdk_env.sh.


可以安装指定版本,安装完成后,一定要调用emsdk activate,不然会出现emcc 无法识别等问题,同一个目录可以同时存在多个版本,使用时调用emsdk activate + 指定版本号就行。

./emsdk install 1.38.45
./emsdk activate 1.38.45


使用前调用一下source emsdk_env.sh,激活环境变量,测试是否安装成功

emcc -v

最后,Webassembly 是一项有趣的技术,它可以将C/C++ 代码编译成字节码,供js端调用,从而达到代码复用的目的。