3

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.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Yuxuan Lu
  • 316
  • 4
  • 13
  • 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 May 21 '20 at 00:29
  • What's the ultimate goal? Allow the user to enter a C program? –  May 21 '20 at 00:33
  • What features of the std libs would you like to use ? – dvhh May 21 '20 at 00:39

4 Answers4

6

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

A T
  • 13,008
  • 21
  • 97
  • 158
  • performance wont be a problem because this is for beginners to run very simple programs, like practicing the 'if' statement. – Yuxuan Lu May 21 '20 at 01:03
  • The CIB is a greate solution, but a little buggy at this moment? A simple helloworld program caused "memory access out of bounds" on my browser. – Yuxuan Lu May 21 '20 at 01:13
2

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

visibleman
  • 3,175
  • 1
  • 14
  • 27
0

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)

0

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

Luke_
  • 745
  • 10
  • 23