VOOZH about

URL: https://thenewstack.io/how-to-compile-c-code-into-webassembly-with-emscripten/

⇱ How to Compile C code into WebAssembly with Emscripten - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2021-08-13 10:54:33
How to Compile C code into WebAssembly with Emscripten
tutorial,
Cloud Native Ecosystem / Software Development

How to Compile C code into WebAssembly with Emscripten

How do I start using WebAssembly?
Aug 13th, 2021 10:54am by Jack Wallen
👁 Featued image for: How to Compile C code into WebAssembly with Emscripten

In our previous entry in this series, we discussed what WebAssmbly was and the main benefits of using WebAssembly. Before we continue with this next part in the series, you’ll definitely want to catch up by first reading What Is WebAssembly and Why Do You Need It?

Once you’ve finished reading that piece, your first question is probably, “How do I start using WebAssembly?” That’s exactly what we’re going to address this time around. I’m going to demonstrate how to get started with this technology. I’ll be doing so on Ubuntu Linux 20.04 (which is also the same process for installing on macOS) and showing you how to run the always helpful “Hello, World!” application. It’s a simple example that will make it easy for you to get started with WebAssembly.

With that said, let’s get on with the steps toward your first WebAssembly app.

Install the Necessary Dependencies

Fortunately, there aren’t many dependencies to take care of. But before we can install the necessary components for WebAssembly, we’ll need to install git with the command:

sudo apt-get install git -y

Once git is installed, you’re ready to prepare your WebAssembly environment.

Install Emscripten

The next step is to use Emscripten, open source software that compiles projects written in C or C++ — or any language that uses LLVM — to browsers, Node.js, or wasm runtimes. The Emscripten SDK profiles all of the necessary tools (such as Clang, Python, and Node.js), as well as an updated mechanism to enable migrating to newer versions of the toolchain as they are released.

What we’re going to do is first compile the code within your user’s home directory. Next, we’ll make this a bit more useful by making it work within the Apache document root.

To download the Empscripten SDK, issue the command (from within your user’s home directory):

git clone https://github.com/emscripten-core/emsdk.git

Once the file download is complete, change into the newly-created directory with the command:

cd emsdk

Next, we’ll make sure our source is updated with the command:

git pull

Now, we can install the latest version of Emscripten with the command:

./emsdk install latest

This will take some time (as it must install a number of tools to your system). You should probably allow up to 30 minutes for this section to complete. Once the installation finishes, you can then activate the latest version with the command:

./emsdk activate latest

Finally, we’ll set the various environmental variables with the command:

source ./emsdk_env.sh

Create your Hello, World! Source

We’re now ready to finally create our Hello, World! application. We’re going to write this app in C, so for many, this might be old hat. Create the new file with the command:

nano hello_world.c

In that file, paste the following code:

#include <stdio.h>
int main(int argc, char ** argv) {
  printf("Hello, New Stack!\n");
}

Notice, the variation on “Hello, World!”?

Save and close the file with the keyboard shortcut [Ctrl]+[x].

Compile the Source Code

What we’re going to do now is use the emcc compiler to take our C code and turn it into a WebAssembly (WASM) HTML file. We’ll do that with the command:

emcc hello_world.c -s WASM=1 -o hello_world.html

Before we actually launch our code, we still need to install a web server and then move the code to our server document root. You could also avoid this by using Emscripten’s built-in HTTP server with the command:

emrun --no_browser --port 8080 hello_world.html

However, to use the above command, you’d have to recompile the code with:

emcc --emrun hello_world.c -s WASM=1 -o hello_world.html

Let’s do this the right way. Install the Apache webserver with the command:

sudo apt-get install apache2 -y

Once that installation finishes, start and enable the Apache webserver service with the commands:

sudo systemctl start apache2

sudo systemctl enable apache2

Next, move the emsdk folder into the Apache document root with the command:

sudo mv emsdk /var/www/html

Now, point your web browser to http://localhost/emsdk/hello_world.html and you should see Hello, New Stack! printed out (Figure 1).

👁 Image

Figure 1: Hello, New Stack!

If you’ve installed Emscripten on a server (without a GUI), you could point the web browser to http://SERVER/emsdk/hello_world.html (where SERVER is the IP address or domain of the server) and you’ll see the same results.

It’s important to note, however, once you move the emsdk folder into the document root, you won’t be able to compile new code into WebAssembly, as the environment variables will have changed. Because of this, you’ll want to start over with the process again, only this time using sudo (as your standard user won’t have permission to install within the /var/www/html/ directory). To do that, you would change to the root user with the command:

sudo -s

Once you’ve done that, you can then change into the document root with the command:

cd /var/www/html/

Remove the emsdk folder (if you already moved it into the document root) with the command:

rm -rf emsdk

And then start at the beginning, back with the command:

sudo git clone https://github.com/emscripten-core/emsdk.git

Go through the how-to once again, and you should be able to compile your code within the Apache document root and then launch it from your web browser.

And that’s the gist of how you compile C code into HTML using WebAssembly, with the help of Emscripten.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.