1

so i am working on this project that lets user write and compile c/c++ code on their mobile, but i am unable to find a way to compile the written c code to generate output.I am basically using angular with ionic framework for mobile app

i read about webassembly but it works by precompiling code and cannot be used under this scenario as per my understanding, i also read about ndk which could work in theory but is there any better way ? I also searched through github but could not find anything useful

Shivam
  • 11
  • 1
  • 1
    Check out https://bellard.org/tcc/ - I think that could be an option for you – Morten Jensen Feb 06 '23 at 14:22
  • So, to clarify: you're looking for a C compiler that runs on mobile platforms (and builds native executables)? – John Bollinger Feb 06 '23 at 14:27
  • Also, you wrote "C" in the title and tagged C, but in the text you say "c/c++". There is no such language is c/c++. If you want *both* a C compiler and a C++ compiler then that's a bigger ask. – John Bollinger Feb 06 '23 at 14:28
  • "lets user write and compile c/c++ code on their mobile." What are those users going to do with the output? What is your use case? – Ruud Helderman Feb 06 '23 at 14:29

1 Answers1

1

If I understand your question correctly, you want to make something like this. Going off of that assumption, you have two ways to go about this problem:

1. Client-side compilation:

In order to do this, you'll have to get your hands on a WASM binary of a C compiler, that supports WASM output. AFAIK no one is distributing WASM binaries (yet), so you'll have to compile a WASM compiler with itself, and if you have to, you'll have to compile the compiler itself with another compiler. This might involve changing up the source code in the following ways:

  • Change up the main function take input from stdin and output the binary to stdout, so that JavaScript (Angluar) can access it.
  • Create an interface function, for example char* js_compile(char* source), which, as you can probably see, will take the source code, execute main with the appropriate arguments, and return the binary.

This method will be the best architecturally, since you won't need to host a server, and the client will be responsible for compiling his own code. However, as you can probably see, this is quite a difficult method, and doing it will probably take several years off of your life.

2. Server-side compilation:

With this method, you'll create a backend (it can be in any language/framework, but I prefer NodeJS/ExpressJS). Then, the backend will expose a HTTP post route (for example, /compile), that will read the body as source. Then, the server will run the code trough a compiler, installed on the server. Finally, the compiler will either respond with the WASM bytecode, the result of the compilation, or with the errors, that the compilers has raised during the compilation.

This of course is the easiest method, but you'll need to host (and probably pay) for a server. If you're OK with that, this is the method you should use.

3. Client-side C interpreter

Here, you'll need to get your hands on a C interpreter, written in JavaScript. Unfortunately, AFAIK, there aren't any COMPLETE C interpreters, but if you're ok with that, you can take a look at this thread. Another solution would be to write an interpreter yourself, but good luck with that.

TL; DR: Pretty complicated, either use a modified WASM compiler on the client, compiler on the server, or interpret the code.

P.S.: If you want me to elaborate on anything, make sure to comment on this post

TopchetoEU
  • 697
  • 5
  • 9