WebAssembly

WebAssembly

安装

Linux

  1. 安装依赖

    apt-get update
    apt-get -y install git build-essential python pip
    
  2. 获取 emsdk

    # Get the emsdk repo
    git clone https://github.com/emscripten-core/emsdk.git
    
  3. 安装配置 Emscripten

    # Enter that directory
    cd emsdk
    
    # Fetch the latest registry of available tools.
    # ./emsdk update
    
    # Download and install the latest SDK tools. Need install Python first. 
    ./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
    
    # Verifying Emscripten
    emcc -v
    
  4. 创建 hello.c

    #include <stdio.h>
    
    int main() {
        printf("hello, world!\n");
        return 0;
    }
    
  5. 编译

    # 生成 a.out.js, a.out.wasm
    emcc hello.c
    
    # 生成 hello.js
    emcc hello.c -o hello.js
    
    # 生成 hello.html 和 hello.js
    emcc hello.c -o hello.html
    
  6. 运行

    node a.out.js