最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to run C program locally in a browser? - Stack Overflow

programmeradmin3浏览0评论

Yes, there is a duplicate problem, but it was asked 5 years ago and haven't been updated for a long time.

In 2020, with the development of WebAssembly, is there a way to compile and run a simple C program locally in the browser?

There is a platform called WasmFiddle which can compile C to wasm in browser, but it lacks the support of standard libraries, such as stdio.h. I think we can implement standard librarys in js and maybe export it to wasm? But this requires lots of work.

My original goal is to build a web-based IDE for students to learn C programming without costing a lot on servers for remote running. So, only libraries like stdio.h, math.h, string.h are required.

UPDATE: this seems like a great implementation of libc to wasm.

High performance is not required, so wasm-based solutions and maybe a VM running c implemented in JS are both greate solutions.

Yes, there is a duplicate problem, but it was asked 5 years ago and haven't been updated for a long time.

In 2020, with the development of WebAssembly, is there a way to compile and run a simple C program locally in the browser?

There is a platform called WasmFiddle which can compile C to wasm in browser, but it lacks the support of standard libraries, such as stdio.h. I think we can implement standard librarys in js and maybe export it to wasm? But this requires lots of work.

My original goal is to build a web-based IDE for students to learn C programming without costing a lot on servers for remote running. So, only libraries like stdio.h, math.h, string.h are required.

UPDATE: this seems like a great implementation of libc to wasm.

High performance is not required, so wasm-based solutions and maybe a VM running c implemented in JS are both greate solutions.

Share Improve this question edited May 21, 2020 at 12:50 sepp2k 370k56 gold badges680 silver badges682 bronze badges asked May 21, 2020 at 0:25 Yuxuan LuYuxuan Lu 4211 gold badge5 silver badges18 bronze badges 4
  • Running a C program in a browser using the standard library doesn't make any sense. Even if you had the whole C library available, you wouldn't be able to use it. The browser doesn't allow interacting with the system directly from its JavaScript runtime, it's a security measure. – Marco Bonelli Commented May 21, 2020 at 0:29
  • What's the ultimate goal? Allow the user to enter a C program? – user5734311 Commented May 21, 2020 at 0:33
  • What features of the std libs would you like to use ? – dvhh Commented May 21, 2020 at 0:39
  • Browsers that support WebGL can run GLSL functions locally. GLSL is compatible with a small subset of the C programming language. – Anderson Green Commented Jun 19, 2024 at 19:54
Add a comment  | 

4 Answers 4

Reset to default 10

Emscripten and WASM are the two popular solutions here.

Don't expect great performance, but you should then be able to link it up with a little bit of JavaScript, CSS and HTML for your code editing and console views.

If you're okay with running a server, then you can use this Jupyter Notebook kernel: https://github.com/jupyter-xeus/xeus-cling

Here's an example in WASM, with no server: https://github.com/tbfleming/cib

For getting your mind blown a tiny bit with regards to what can be done with emscripten and other related techniques, check out the work of Fabrice Bellard, on Tiny C Compiler, Tiny Emu, and JsLinux.

JsLinux, basically runs virtualized machines in JavaScript, and Bellard has examples of running both Linux, and Windows2000

You can go nuclear and just put everything in a VM, clang as binary runs on some input code and compile some binary file as output, than is then again run in the VM. All client-side, with some performance degradation due to the virtualization.

Example: https://twitter.com/alexpignotti/status/1261357985617469442?s=20 (a colleague of mine working on exactly this problem)

You've got 2 options, either you get some sort of VM that runs in a browser that is capable of running some sort of architecture to which C can compile, or you compile to WASM using EMScripten.

WASM is obviously the better choise since its nativly supported in all the (decent) browsers

发布评论

评论列表(0)

  1. 暂无评论