diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-06-04 11:55:34 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-06-04 11:55:34 +0200 |
| commit | 2a8e7aeeef99161b6f0378ac14e297c23ca6227b (patch) | |
| tree | d3ab195eabf4d06cad8b305756e14d4b138eae63 /README.md | |
| download | emscripten-tutorial-2a8e7aeeef99161b6f0378ac14e297c23ca6227b.tar.gz emscripten-tutorial-2a8e7aeeef99161b6f0378ac14e297c23ca6227b.zip | |
First 4 examples
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..4326298 --- /dev/null +++ b/README.md | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | # How to port a complex C codebase to the web | ||
| 2 | |||
| 3 | *This repository is still work in progress* | ||
| 4 | |||
| 5 | This repository contains the source code for the examples discussed in | ||
| 6 | a post walkthrough that is going to appear shortly on my website. | ||
| 7 | It will be linked here once published. | ||
| 8 | |||
| 9 | Each sub-folder is a self-contained example of a C program (or library) | ||
| 10 | that can be compiled to [WebAssembly](https://webassembly.org/) using | ||
| 11 | [Emscripten](https://emscripten.org), and some JavaScript and HTML code | ||
| 12 | that can be used to run the C or C++ code in a web page or in a JavaScript | ||
| 13 | runtime such as [Node.js](https://nodejs.org). Following these steps in | ||
| 14 | order will walk you through the process of deploying a complex C or C++ | ||
| 15 | program (including multithreading, persistent storage, callback functions | ||
| 16 | and so on) as a web app. | ||
| 17 | |||
| 18 | Besides the source files, each folder contains a few one-line shell | ||
| 19 | scripts, for convenience: | ||
| 20 | |||
| 21 | * `build.sh`: to build the C / C++ code with Emscripten. | ||
| 22 | * `run-node.sh`: to run an example program in Node.js. | ||
| 23 | * `run-server.sh`: to start a web server on | ||
| 24 | [localhost:8080](http://localhost:8080) running an example web page | ||
| 25 | (require darkhttpd, see below). | ||
| 26 | |||
| 27 | The examples have been tested only on Linux, but should work on any | ||
| 28 | UNIX system, and should be easy to adapt to Windows or other OSes. | ||
| 29 | Pull requests are welcome. | ||
| 30 | |||
| 31 | ## Prerequisites | ||
| 32 | |||
| 33 | In order to follow this tutorial, you are going to need the following: | ||
| 34 | |||
| 35 | 1. [Emscripten](https://emscripten.org) | ||
| 36 | 2. A web server to run locally, such as | ||
| 37 | [darkhttpd](https://github.com/emikulic/darkhttpd) | ||
| 38 | 3. [Node.js](https://nodejs.org) (this is optional, since a version | ||
| 39 | of Node.js is distributed together with emscripten). | ||
| 40 | |||
| 41 | ## 0. Hello world | ||
| 42 | |||
| 43 | The folder `00_hello_world` contains the simplest possible example. | ||
| 44 | |||
| 45 | ## 1. Building a library | ||
| 46 | |||
| 47 | A common use case for building C or C++ code to WebAssembly is using some | ||
| 48 | high-performance library in a web app. In this situation, the code you | ||
| 49 | want to build does not have a `main()` entry point, but instead its public | ||
| 50 | functions are called from JavaScript. Motivated by this, in the folder | ||
| 51 | `01_library` you'll find an extremely simple C library (with only one | ||
| 52 | one-line function), and some JavaScript code to run it from a web page. | ||
| 53 | |||
| 54 | ## 2. Making it a module | ||
| 55 | |||
| 56 | In `02_library_modularized` we review the previous example and we build it | ||
| 57 | into a | ||
| 58 | [module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). The result is the same, but more convenient to use. | ||
| 59 | |||
| 60 | ## 3. Multi-threading | ||
| 61 | |||
| 62 | In `03_threads` we build a more complicated example based on | ||
| 63 | [pthreads](https://en.wikipedia.org/wiki/Pthreads). To run this | ||
| 64 | example, the web server has to be configured to provide the correct | ||
| 65 | `Cross-Origin-*` headers, see `03_threads/run-server.sh` for details. | ||
