编译demo

145 阅读1分钟

编译 emsdk

  • 拉取仓库
git clone https://github.com/emscripten-core/emsdk.git
  • 如果之前 clone 过,那么这里更新最新的代码
git pull
  • 下载和安装最新的 SDK 工具
./emsdk install latest
  • 为当前的 user 激活最新的 SDK 工具,在 .emscripten 文件中写入当前用户
./emsdk activate latest
  • 将 SDK 相关的命令加入到 PATH,以及激活其他环境变量
source ./emsdk_env.sh
  • 检查是否安装完成
emcc -v 

第一个小demo

  • 创建 hello.c
#include <stdio.h>

int main()
{
    printf("hello, world");
}
  • 编译
emcc hello.c -s EXIT_RUNTIME=1 -s WASM=1 -o hello.html

-s EXIT_RUNTIME=1 表示

-s WASM=1 表示

-o hello.html 表示

第二个小demo

  • 创建 hello2.c
#include <stdio.h>

int main()
{
    printf("hello, world");
}
  • 编译
emcc -o hello2.html hello2.c -O3 -s WASM=1 -s EXIT_RUNTIME=1 --shell-file html_template/shell_minimal.html