安裝依賴
Node.js(0.8 or above; 0.10.17 or above to run websocket-using servers in node):
Python2.x (2.7.3 or above preferred)
Java(1.6.0_31 or later). Java is optional. It is required to use theClosure Compiler(in order to minify your code).
Gitclient. Git is required if building tools from source.
Fastcomp(Emscripten’s fork of LLVM and Clang)
參考鏈接地址:Manually Building Emscripten on Mac OS X
To build the Fastcomp code from source:
Create a directory to store the build. It doesn’t matter where, because Emscripten gets the information from thecompiler configuration file (~/.emscripten). We show how to update this file later in these instructions:
mkdirmyfastcompcdmyfastcomp
Clone the fastcomp LLVM repository (https://github.com/kripken/emscripten-fastcomp):
gitclonehttps://github.com/kripken/emscripten-fastcomp
Clone thekripken/emscripten-fastcomp-clangrepository intoemscripten-fastcomp/tools/clang:
cdemscripten-fastcompgitclonehttps://github.com/kripken/emscripten-fastcomp-clang tools/clang
Warning
Youmustclone it into a directory namedclangas shown, so thatClangis present intools/clang!
Create abuilddirectory (inside theemscripten-fastcompdirectory) and then navigate into it:
mkdirbuildcdbuild
Configure the build usingeithercmakeor theconfigurescript:
Usingcmake:
cmake..-DCMAKE_BUILD_TYPE=Release-DLLVM_TARGETS_TO_BUILD="X86;JSBackend"-DLLVM_INCLUDE_EXAMPLES=OFF-DLLVM_INCLUDE_TESTS=OFF-DCLANG_INCLUDE_EXAMPLES=OFF-DCLANG_INCLUDE_TESTS=OFF
Note
On Windows add the-G"VisualStudio10Win64"directive to build using Visual Studio (Visual Studio 2011 and 2012 do NOT work).
Usingconfigure(Linux/Mac only):
../configure--enable-optimized--disable-assertions--enable-targets=host,js
Determine the number of available cores on your system (Emscripten can run many operations in parallel, so using more cores may have a significant impact on compilation time):
On Mac OS X you can get the number of cores using:Apple menu | About this mac | More info | System report. TheHardware overviewon the resulting dialog includes aTotal number of coresentry.
On Linux you can find the number of cores by entering the following command on the terminal:cat/proc/cpuinfo|grep"^cpucores"|uniq.
On Windows the number of cores is listed on theTask Manager | Performance Tab. You can open theTask Managerby enteringCtrl + Shift + Escfrom the Desktop.
Callmaketo build the sources, specifying the number of available cores:
make-j4
Note
If the build completes successfully,clang,clang++, and a number of other files will be created in the release directory (/build/Release/bin).
The final step is to update the~/.emscriptenfile, specifying the location offastcompin theLLVM_ROOTvariable.
Note
If you’re building thewholeof Emscripten from source, following the platform-specific instructions inBuilding Emscripten from Source, you won’t yet have Emscripten installed. In this case, skip this step and return to those instructions.
If you already have an Emscripten environment (for example if you’re building Fastcomp using the SDK), then setLLVM_ROOTto the location of theclangbinary under thebuilddirectory. This will be something like/build/Release/binor/build/bin:
```
vim ~/.emscripten
```
修改 LLVM_ROOT到指定的文件目錄
LLVM_ROOT = '/usr/local/myfastcomp/emscripten-fastcomp/build/bin' # directory
TheEmscripten code, from GitHub
clone emscripten項(xiàng)目到本地
```
git clone https://github.com/kripken/emscripten
cd emscripten
npm install
```
測(cè)試是否各依賴環(huán)境已經(jīng)正確安裝成功
在emscripten目錄下運(yùn)行
```
./emcc tests/hello_world.cpp
```
如果沒(méi)有報(bào)錯(cuò)則會(huì)在同目錄下找到一個(gè)新文件a.out.js
現(xiàn)在可以通過(guò)nodejs來(lái)運(yùn)行a.out.js這個(gè)文件了
```
node a.out.js
```
會(huì)在控制臺(tái)打印出
```
hello, world!
```
通過(guò)browserify編譯使之能在瀏覽器運(yùn)行
安裝browserify
```
sudo npm install browserify -g
```
編譯a.out.js文件
```
browserify a.out.js > test.js
```
現(xiàn)在可以在網(wǎng)頁(yè)中引入test.js文件
```
<script src='test.js'></script>
```
打開(kāi)控制臺(tái)可以看到
```
hello world
```
可以在輸出的時(shí)候直接指定聲稱為瀏覽器端運(yùn)行的代碼,
./emcc tests/hello_world.cpp -o test.html
在js中調(diào)用c++/c寫的函數(shù)
Module.ccap("function_name", return_type, arg_type, arg)
使用emscritpen輸入代碼優(yōu)化
./emcc tests/hello-test.cpp -o function.js
代碼是通過(guò)指定優(yōu)化的優(yōu)化參數(shù)運(yùn)行時(shí),EMCC叨襟。級(jí)別包括:-O0(不優(yōu)化)菩貌,-O1项棠,-O2拉队,-Os秆乳,-OZ和-O3
添加setting
-s EXPORTED_FUNCTIONS="['_uncompress']"? //到處函數(shù)
-s? NO_FILESYSTEM=1? ? ? //0 在代碼中包含文件系統(tǒng)代碼般妙, 1在代碼中不包含文件系統(tǒng)代碼
-s EXPORTED_RUNTIME_METHODS? ? //到處可以在模塊中使用的函數(shù)
[
'FS_createFolder',
'FS_createPath',
'FS_createDataFile',
'FS_createPreloadedFile',
'FS_createLazyFile',
'FS_createLink',
'FS_createDevice',
'FS_unlink',
'Runtime',
'ccall',
'cwrap',
'setValue',
'getValue',
'ALLOC_NORMAL',
'ALLOC_STACK',
'ALLOC_STATIC',
'ALLOC_DYNAMIC',
'ALLOC_NONE',
'allocate',
'getMemory',
'Pointer_stringify',
'AsciiToString',
'stringToAscii',
'UTF8ArrayToString',
'UTF8ToString',
'stringToUTF8Array',
'stringToUTF8',
'lengthBytesUTF8',
'stackTrace',
'addOnPreRun',
'addOnInit',
'addOnPreMain',
'addOnExit',
'addOnPostRun',
'intArrayFromString',
'intArrayToString',
'writeStringToMemory',
'writeArrayToMemory',
'writeAsciiToMemory',
'addRunDependency',
'removeRunDependency',
];
編譯多個(gè)c++/c文件到一個(gè)js中
# Sub-optimal - JavaScript optimizations are omitted
./emcc -O2 a.cpp -o a.bc
./emcc -O2 b.cpp -o b.bc
./emcc a.bc b.bc -o project.js
# Sub-optimal - LLVM optimizations omitted
./emcc a.cpp -o a.bc
./emcc b.cpp -o b.bc
./emcc -O2 a.bc b.bc -o project.js
# Broken! Different JavaScript and LLVM optimisations used.
./emcc -O1 a.cpp -o a.bc
./emcc -O2 b.cpp -o b.bc
./emcc -O3 a.bc b.bc -o project.js
# Correct. The SAME LLVM and JavaScript options are provided at both levels.
./emcc -O2 a.cpp -o a.bc
./emcc -O2 b.cpp -o b.bc
./emcc -O2 a.bc b.bc -o project.js