Compile C to WebAssembly and Run in Browser

work

WebAssembly (abbreviated wasm) is a low-level binary format designed for compiling high-level programming languages such as C, C++ and Rust. Its main goal is to allow code to execute in browsers with near native performance, offering a fast, compact, and secure alternative to JavaScript.

In this article, we’ll cover the basics of WebAssembly and walk through step-by-step how to set up the environment, compile a simple program, and run it in a browser.

What you need to know before you start

To work with WebAssembly, it is desirable to understand the basic concepts of web development – HTML, JavaScript, and DOM. It will also be helpful if you are familiar with languages such as C or Rust, but it is not mandatory.

WebAssembly fundamentals

Before we get into practice, let’s briefly go over the key features of WebAssembly:

  • Binary format. The code in WebAssembly is presented in a compact form, which speeds up its loading, decoding and execution in the browser.
  • Linear memory. WebAssembly uses a single block of memory that is scalable. This memory is accessible both from within the wasm module and from JavaScript.
  • Imports and exports. WebAssembly modules can import and export functions, variables, and memory, making it easy to integrate wasm into existing JavaScript code.

Setting up the environment

To get started, you need to install tools to compile and run wasm code. One of the most popular options is to use Emscripten, which is suitable for C and C++.

The process involves the following steps:

  • Installing Emscripten (following the official instructions).
  • Activating the Emscripten environment in the terminal or command line to use its tools.

Writing and compiling a simple program

After setting up the environment, you can start creating a project. You need to create a project directory and in it a file with a simple program that, for example, displays a message. Using Emscripten you can compile such a file into WebAssembly and get an HTML page to run in a browser.

When you run the resulting file, you will see the result of the program execution in the browser console.

Interaction with JavaScript

One of the key advantages of WebAssembly is the ability to tightly integrate with JavaScript. You can call wasm functions from JavaScript and vice versa, and share memory between them.

To give an example, you can extend the initial program by adding a simple mathematical function such as adding numbers. Once compiled with the right settings, the function will become available from JavaScript, where it can be called and the result obtained.

This allows WebAssembly to be used as a productivity module inside standard web applications.

Conclusion

In this article, we learned the basics of WebAssembly: what it is, how to prepare the environment, create and compile the first program, and link it to JavaScript.

WebAssembly is a powerful tool for speeding up web applications and extending their capabilities. It is especially useful in tasks where performance is critical, such as graphics, data processing algorithms, or real-time computing.