-
工具链选择Emscripten 先安装emsdk,该工具包含了编译C++为WASM的所有工具,包括编译器
git clone https://github.com/emscripten-core/emsdk.git cd emsdk -
克隆后下载安装最新的工具链:
# windows ./emsdk.bat install latest ./emsdk.bat activate latest # powershell,其他bash使用.bat ./emsdk_env.ps1 #linux/masOS ./emsdk install latest ./emsdk activate latest source ./emsdk_env.sh想要长期使用可配置环境变量
-
验证安装
emcc -v -
任意编写一个Hello.cpp
#include <iostream> int main() { std::cout << "Hello, World from C++ and WASM!" << std::endl; return 0; } -
编译
emcc hello.cpp -o hello.html -
运行
首先启动一个http服务(在编译好的html位置)
python -m http.server 8080然后在浏览器上打开http://localhost:8080/hello.html 最后得到了: