EMCC 系列01

Install

官文: https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html

1
2
3
4
5
6
7
8
9
git clone https://github.com/emscripten-core/emsdk.git

cd emsdk
git pull
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh

emcc -v

需要 xcode + command line tools, git, cmake, nodejs, python

Hello World ~

1
2
3
4
5
6
#include <stdio.h>

int main() {
printf("hello, world!\n");
return 0;
}
1
2
3
4
emcc a.c

ls
node a.out.js

浏览器运行

1
2
emcc a.c -o index.html
python -m SimpleHTTPServer 8080

打开浏览器访问 http://127.0.0.1:8080

预加载文件

浏览器内部的文件系统是一个沙盒概念, 无法与外部系统直接交互

1
emcc a.c -o index.html --preload-file somefile.txt

js 压缩比

在生成 js 文件的时候, 默认是不压缩的

但是在测试或者上线发布的时候, 为了追求包体大小, 一般传统的前端是要做压缩混淆的

1
2
3
emcc a.c        # 不压缩
emcc -O1 a.c # 简单压缩
emcc -O2 a.c # 极致压缩

Tips: 这里注意是大写的, 不是或者小写.

Donate - Support to make this site better.
捐助 - 支持我让我做得更好.