如何在瀏覽器端運(yùn)行c/c++語(yǔ)言編寫的代碼

安裝依賴

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',

];

Building Projects

編譯多個(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末纪铺,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子碟渺,更是在濱河造成了極大的恐慌鲜锚,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件苫拍,死亡現(xiàn)場(chǎng)離奇詭異芜繁,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)绒极,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門浆洗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人集峦,你說(shuō)我怎么就攤上這事伏社。” “怎么了塔淤?”我有些...
    開(kāi)封第一講書人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵摘昌,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我高蜂,道長(zhǎng)聪黎,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任备恤,我火速辦了婚禮稿饰,結(jié)果婚禮上锦秒,老公的妹妹穿的比我還像新娘。我一直安慰自己喉镰,他們只是感情好旅择,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著侣姆,像睡著了一般生真。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上捺宗,一...
    開(kāi)封第一講書人閱讀 51,190評(píng)論 1 299
  • 那天柱蟀,我揣著相機(jī)與錄音,去河邊找鬼蚜厉。 笑死长已,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的昼牛。 我是一名探鬼主播术瓮,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼匾嘱!你這毒婦竟也來(lái)了斤斧?” 一聲冷哼從身側(cè)響起早抠,我...
    開(kāi)封第一講書人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤霎烙,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后蕊连,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體悬垃,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年甘苍,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了尝蠕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡载庭,死狀恐怖看彼,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情囚聚,我是刑警寧澤靖榕,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站顽铸,受9級(jí)特大地震影響茁计,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜谓松,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一星压、第九天 我趴在偏房一處隱蔽的房頂上張望践剂。 院中可真熱鬧,春花似錦娜膘、人聲如沸逊脯。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)男窟。三九已至,卻和暖如春贾富,著一層夾襖步出監(jiān)牢的瞬間歉眷,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工颤枪, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留汗捡,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓畏纲,卻偏偏與公主長(zhǎng)得像扇住,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子盗胀,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 前言 2000年艘蹋,伊利諾伊大學(xué)厄巴納-香檳分校(University of Illinois at Urbana-...
    星光社的戴銘閱讀 15,894評(píng)論 8 180
  • 編譯器做些什么? 本文主要探討一下編譯器主要做些什么票灰,以及如何有效的利用編譯器女阀。 簡(jiǎn)單的說(shuō),編譯器有兩個(gè)職責(zé):把 ...
    評(píng)評(píng)分分閱讀 1,128評(píng)論 1 5
  • This chapter discusses some of the design decisions that ...
    狂風(fēng)無(wú)跡閱讀 972評(píng)論 0 0
  • 比較麻煩,未完待續(xù)惹盼。庸汗。。手报。蚯舱。。 VIM 配置:.vimrc以及.ycm_extra_conf.py配置 插件:個(gè)人...
    陳半仙兒閱讀 16,182評(píng)論 3 26
  • 你笑了掩蛤,是天使枉昏。 你哭了,是天使盏档。 你生氣了凶掰,是天使。 你睡覺(jué)了,是天使懦窘。 你很乖前翎,是天使。 你吵鬧畅涂,是需要安撫的...
    yiyi_4f59閱讀 151評(píng)論 0 0