Yuxuan Lu
Yuxuan Lu

Reputation: 421

Is there a way to run C program locally in a browser?

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.

Upvotes: 7

Views: 8772

Answers (4)

Luke_
Luke_

Reputation: 805

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

Upvotes: 1

Carlo Piovesan
Carlo Piovesan

Reputation: 332

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)

Upvotes: 1

visibleman
visibleman

Reputation: 3315

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

Upvotes: 4

A T
A T

Reputation: 13836

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

Upvotes: 10

Related Questions