安装 Emscripten编译器
使用SDK方式安装更方便
解压下载的zip,进入SDK的解压目录。逐步执行如下命令
# Fetch the latest registry of available tools.
./emsdk update
# 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
注意
最后一行非常重要。每次重新登陆或者新建 Shell 窗口,都要执行一次这行命令source ./emsdk_env.sh。否则,命令就失效了。小坑小坑
demo演示 - 将c编译成js
- 创建c文件 hello.c
#include <stdio.h>
int main() {
printf("hello, world!\n");
return 0;
}
- 将这个程序转成 asm.js。
emcc hello.c
发现会生成文件 a.out.js
- 执行js文件
node a.out.js
输出 :
hello, world!