Emscripten (0) Emscripten 安裝和 Hello world

Emscripten 是一個轉譯工具,能幫你把 C code 轉成 JS。

1. 安裝 Emscripten:

先去下載 Emscripten。

$ git clone https://github.com/juj/emsdk.git
$ cd emsdk

安裝

$ ./emsdk update
$ ./emsdk install latest
$ ./emsdk activate latest
$ source ./emsdk_env.sh

編輯 ~/.bashrc,這步就是看你 "source ./emsdk_env.sh" 產生哪些變數把它們都加進 ~/.bashrc 裡。

#[start-20180801-ben(emsdk)-add]#
export PATH=$PATH:/home/ben/ProgTest/emsdk:/home/ben/ProgTest/emsdk/clang/e1.38.10_64bit:/home/ben/ProgTest/emsdk/node/8.9.1_64bit/bin:/home/ben/ProgTest/emsdk/emscripten/1.38.10

export EMSDK=/home/ben/ProgTest/emsdk
export EM_CONFIG=/home/ben/.emscripten
export LLVM_ROOT=/home/ben/ProgTest/emsdk/clang/e1.38.10_64bit
export EMSCRIPTEN_NATIVE_OPTIMIZER=/home/ben/ProgTest/emsdk/clang/e1.38.10_64bit/optimizer
export BINARYEN_ROOT=/home/ben/ProgTest/emsdk/clang/e1.38.10_64bit/binaryen
export EMSDK_NODE=/home/ben/ProgTest/emsdk/node/8.9.1_64bit/bin/node
export EMSCRIPTEN=/home/ben/ProgTest/emsdk/emscripten/1.38.10
#[end-20180801-ben(emsdk)-add]#

2. Hello world:

編輯一個 hello.c

#include <stdio.h>

int main() {
  printf("Hello, world!\n");
  return 0;
}

將其 JS 化,並用 node 執行。

$ emcc hello.c
$ node a.out.js

執行結果。

Hello, world!

If an error occurs when calling emcc, run it with the -v option to print out a lot of useful debug information.

Older node.js versions do not have WebAssembly support yet. In that case you will see an error message suggesting that you build with -s WASM=0 to disable WebAssembly, and then emscripten will emit the compiled code as JavaScript.

留言

熱門文章