安装依赖

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)

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

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项目到本地

```

git clone https://github.com/kripken/emscripten

cd emscripten

npm install

```

测试是否各依赖环境已经正确安装成功

在emscripten目录下运行

```

./emcc tests/hello_world.cpp

```

如果没有报错则会在同目录下找到一个新文件a.out.js

现在可以通过nodejs来运行a.out.js这个文件了

```

node a.out.js

```

会在控制台打印出

```

hello, world!

```

通过browserify编译使之能在浏览器运行

安装browserify

```

sudo npm install browserify -g

```

编译a.out.js文件

```

browserify a.out.js > test.js

```

现在可以在网页中引入test.js文件

```

```

打开控制台可以看到

```

hello world

```

可以在输出的时候直接指定声称为浏览器端运行的代码,

./emcc tests/hello_world.cpp -o test.html

在js中调用c++/c写的函数

Module.ccap("function_name", return_type, arg_type, arg)

使用emscritpen输入代码优化

./emcc tests/hello-test.cpp -o function.js

代码是通过指定优化的优化参数运行时,EMCC。级别包括:-O0(不优化),-O1,-O2,-Os,-OZ和-O3

添加setting

-s EXPORTED_FUNCTIONS="['_uncompress']"  //到处函数

-s  NO_FILESYSTEM=1      //0 在代码中包含文件系统代码, 1在代码中不包含文件系统代码

-s EXPORTED_RUNTIME_METHODS    //到处可以在模块中使用的函数

[

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

];

编译多个c++/c文件到一个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

浏览器可以用c语言编辑吗,如何在浏览器端运行c/c++语言编写的代码相关推荐

  1. 为什么c语言写程序要挂起,我运行的C语言程序挂起了,应该怎么办?

    导航:网站首页 > 我运行的C语言程序挂起了,应该怎么办? 时间:2017-11-14 相关问题: 匿名网友: 当你运行一个程序时会有多种原因使它挂起,这些原因可以分为以下4种基本类型: (1) ...

  2. c语言源程序不编译也能直接运行吗,c语言的源程序不必通过编译就可以执行对吗...

    错误,C语言采用编译方式将源程序转换为二进制的目标代码,编写好一个C程序到完成运行一般经过以下几个步骤,编辑和编译,就是将已经编辑好的源程序翻译成二进制的目标代码,经编译后的得到的二进制代码还不能直接 ...

  3. 大二c语言期末考试题库及详解答案,大学C语言期末考试练习题(带详解答案)...

    <大学C语言期末考试练习题(带详解答案)>由会员分享,可在线阅读,更多相关<大学C语言期末考试练习题(带详解答案)(55页珍藏版)>请在金锄头文库上搜索. 1.一. 单项选择题 ...

  4. r语言 编辑 d3.js_d3.js的语言介绍

    r语言 编辑 d3.js by Matt Oxley 由马特·奥克斯利(Matt Oxley) d3.js的语言介绍 (A linguistic introduction to d3.js) 如何从一 ...

  5. c语言fac函数求n的阶乘,急求C语言编辑题:Cnm=n!/m!(n-m)!其中n,m 由键盘输入。要求设计一个函数fac(n)求某个正整数n 的阶乘。...

    急求C语言编辑题:Cnm=n!/m!(n-m)!其中n,m 由键盘输入.要求设计一个函数fac(n)求某个正整数n 的阶乘. 來源:互聯網  2010-05-29 01:44:10  評論 分類: 電 ...

  6. c语言英汉互译编程,用C语言编辑简单英汉互译词典.doc

    疥详刁呆害獭荆羞哈沮蒜赫夜内淮牺彻蔼纤凤虹锥硝够唬古进淋牡振拘铅笺元扳与醒靳蹋销钡胶致石衙钦目妈而炸赚鹤邓穷窍瘴笼旬房殆查恨蠢煌沧祥斥瞩骤敌晤屏莲匆目穷妖暗屹码冬息摊挎傍啡坟范给羹哥皱做斋绥甭焕睫苍苫 ...

  7. c语言编程等腰三角形,用c语言编辑一个等腰三角形的讲解过程 一个等腰三角形怎么平均分成三个面积相等的三角形?...

    导航:网站首页 > 用c语言编辑一个等腰三角形的讲解过程 一个等腰三角形怎么平均分成三个面积相等的三角形? 用c语言编辑一个等腰三角形的讲解过程 一个等腰三角形怎么平均分成三个面积相等的三角形? ...

  8. 使用UltraEdit25.20.0.88进行Verilog语言编辑配置方式(详细)

    UltraEdit版本为25.20.0.88,其他版本也适用. 1:下载Verilog的语法高亮文件. 即可支持相应的语言编辑,关键字将用不同色彩标出. 可以到官方网站去下载,包括上百种语法文件,我想 ...

  9. Ubuntu 14.04下Gedit编辑器设置为多语言编辑及集成开发环境

    Gedit是一个通用的编辑程序,其支持插件及自定义语言设置功能. 以Ubuntu 14.04下的Gedit编辑器为例,介绍其定制方法: 1 安装插件 sudo apt-get install gedi ...

最新文章

  1. [转载]李开复先生给中国学生的第四封信:大学四年应是这样度过
  2. mysql索引图文操作_图文并茂,说说MySQL索引
  3. PHP可以重新定义已经存在的函数的类库patchwork
  4. python day08
  5. base cap 分布式_干货分享:基于本地消息表的分布式事务解决方案总结
  6. Android 9 带着 AI 来了,为什么我们还停留在 6?
  7. php 数组元素 转 变量,php变量与数组相互转换的方法(extract与compact)
  8. ProxySQL Getting started
  9. C++如何写adaptable仿函数
  10. 耗时两天,Html实现小米官网
  11. 【思维导图】万科王石自传《我的改变:个人的现代化四十年》做的一些摘录
  12. 学习浙江大学Photoshop设计精讲精练过程中的重难点及内容收获
  13. Ionic3.x 创建项目中的问题-IonIC start myApp tabs
  14. airpods pro是按压还是触摸_泼冷水:AirPods Pro至今为止媒体们还没提到但影响使用体验的TIPS...
  15. Linux下Oracle移植数据
  16. 我不爱的那个女人[转]
  17. OpenCV内存方式,将rgb数据压缩成jpg数据
  18. Citavi——令人激动的文献管理工具
  19. 批量全景视频画面提取
  20. 5-32 哥尼斯堡的“七桥问题” (25分)

热门文章

  1. 12 岁赚钱买电脑,19 岁创立公司,戴尔传奇
  2. 阿里开源 GNN 框架 Graph-Learn,实现了各类可复用模型和编程接口!
  3. 20 年“码龄”的老程序员如何看编程发展?
  4. 华为发布全球首款 5G 汽车通讯硬件;今日头条系产品大裁员;三星手机推迟上市 | 极客头条...
  5. 00 后的 AI 开发者进阶之道:从入门到鏖战 MIT 编程大赛 | 人物志
  6. 112654 个招聘数据告诉你,程序员 2019 年该去哪!
  7. Linux 末路,Kubernetes 崛起!
  8. 你有程序员朋友吗?告诉他,100 万等他来拿
  9. Windows 7 又惹祸了!
  10. 盘点 | 2017 年关于 Python 的 12 件大事