Emscripten (1) Generating HTML
1. 產生 HTML:
編輯一個 hello.c
#include <stdio.h> int main() { printf("Hello, world!\n"); return 0; }
編譯並輸出成 HTML。
$ emcc hello.c -o hello.html
用 browser 開啟 HTML。
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtEgWJGPnyF2Nb4sJpkiRNwoi2uRx3DkYvyqbr9m9DoE5dBJ62zVadlaTO4sWpzLXDlUHiBJ-XgAPmI9NQyJ-hPpug0CrrqYnplPqFC0JuY79E6SlEeVskyi1Xzv7-7BssTzXzKY5ocGM/s640/2018-08-03+11-09-59+%25E7%259A%2584%25E8%259E%25A2%25E5%25B9%2595%25E6%2593%25B7%25E5%259C%2596.png)
2. 讀取檔案:
編輯 hello.txt。
Hello world Hello alice Hello bob
編輯 fio.c。
#include <stdio.h> int main() { FILE *file = fopen("hello.txt", "rb"); if (!file) { printf("cannot open file\n"); return 1; } while (!feof(file)) { char c = fgetc(file); if (c != EOF) { putchar(c); } } fclose (file); return 0; }
file 要用 preload 的方式編進去。
$ emcc fio.c -o fio.html --preload-file hello.txt
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjS7yEdRnqgIoA7mHVKqN6FeCRM-B-uMeLRXHuQEccsjQ7bRGAhBiwCL4OvGYte9gK4FlteA68Cg4907hsmGjxIgmGwyHZugPjoyt25ezRYnEujpkj542ll2JGOrun_PIwDGGKFEfmzEPE/s640/2018-08-03+11-25-45+%25E7%259A%2584%25E8%259E%25A2%25E5%25B9%2595%25E6%2593%25B7%25E5%259C%2596.png)
JavaScript is usually run in the sandboxed environment of a web browser, without direct access to the local file system.
Emscripten simulates a file system that you can access from your compiled C/C++ code using the normal libc stdio API.
留言
張貼留言