diff options
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 40 |
1 files changed, 21 insertions, 19 deletions
| @@ -1,24 +1,21 @@ | |||
| 1 | # How to port a complex C codebase to the web | 1 | # How to port a complex C codebase to the web |
| 2 | 2 | ||
| 3 | *This repository is still work in progress* | ||
| 4 | |||
| 5 | This repository contains the source code for the examples discussed in | 3 | This repository contains the source code for the examples discussed in |
| 6 | a post walkthrough that is going to appear shortly on my website. | 4 | [this blog post / tutorial](https://sebastiano.tronto.net/blog/2025-06-06-webdev). |
| 7 | It will be linked here once published. | ||
| 8 | 5 | ||
| 9 | Each sub-folder is a self-contained example of a C program (or library) | 6 | Each sub-folder is a self-contained example of a C or C++ program (or |
| 10 | that can be compiled to [WebAssembly](https://webassembly.org/) using | 7 | library) that can be compiled to [WebAssembly](https://webassembly.org/) |
| 11 | [Emscripten](https://emscripten.org), and some JavaScript and HTML code | 8 | using [Emscripten](https://emscripten.org), and some JavaScript and HTML |
| 12 | that can be used to run the C or C++ code in a web page or in a JavaScript | 9 | code that can be used to run this program in a web page or in a JavaScript |
| 13 | runtime such as [Node.js](https://nodejs.org). Following these steps in | 10 | 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++ | 11 | order will walk you through the process of deploying a complex application |
| 15 | program (including multithreading, persistent storage, callback functions | 12 | (including multithreading, persistent storage and callback functions) |
| 16 | and so on) as a web app. | 13 | as a web app. |
| 17 | 14 | ||
| 18 | Besides the source files, each folder contains a few one-line shell | 15 | Besides the source files, each folder contains a few one-line shell |
| 19 | scripts, for convenience: | 16 | scripts, for convenience. Usually, the following are present: |
| 20 | 17 | ||
| 21 | * `build.sh`: to build the C / C++ code with Emscripten. | 18 | * `build.sh`: to compile the C code with Emscripten. |
| 22 | * `run-node.sh`: to run an example program in Node.js. | 19 | * `run-node.sh`: to run an example program in Node.js. |
| 23 | * `run-server.sh`: to start a web server on | 20 | * `run-server.sh`: to start a web server on |
| 24 | [localhost:8080](http://localhost:8080) running an example web page | 21 | [localhost:8080](http://localhost:8080) running an example web page |
| @@ -43,7 +40,9 @@ In order to follow this tutorial, you are going to need the following: | |||
| 43 | 40 | ||
| 44 | ## 0. Hello world | 41 | ## 0. Hello world |
| 45 | 42 | ||
| 46 | The folder `00_hello_world` contains the simplest possible example. | 43 | The folder `00_hello_world` contains the simplest possible example: |
| 44 | a C program that prints "Hello world" to standard output. The default | ||
| 45 | Emscripten HTML template is used to run this code in a web page. | ||
| 47 | 46 | ||
| 48 | ## 1. Building a library | 47 | ## 1. Building a library |
| 49 | 48 | ||
| @@ -56,9 +55,10 @@ one-line function), and some JavaScript code to run it from a web page. | |||
| 56 | 55 | ||
| 57 | ## 2. Making it a module | 56 | ## 2. Making it a module |
| 58 | 57 | ||
| 59 | In `02_library_modularized` we review the previous example and we build it | 58 | In `02_library_modularized` we review |
| 60 | into a | 59 | the previous example and we build it into a |
| 61 | [module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). The result is the same, but more convenient to use. | 60 | [module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). |
| 61 | The result is the same, but it is more convenient to use. | ||
| 62 | 62 | ||
| 63 | ## 3. Multi-threading | 63 | ## 3. Multi-threading |
| 64 | 64 | ||
| @@ -75,6 +75,8 @@ thread by using a web worker. | |||
| 75 | ## 5. Callback functions | 75 | ## 5. Callback functions |
| 76 | 76 | ||
| 77 | In `05_callback` the example is extended to include a callback function. | 77 | In `05_callback` the example is extended to include a callback function. |
| 78 | The C program uses this function to log its progress back to the | ||
| 79 | JavaScript side. | ||
| 78 | 80 | ||
| 79 | ## 6. Storage | 81 | ## 6. Storage |
| 80 | 82 | ||
| @@ -85,6 +87,6 @@ using as backend the | |||
| 85 | This example only works in the browser, as other runtimes (such as | 87 | This example only works in the browser, as other runtimes (such as |
| 86 | Node.js) require different backends. | 88 | Node.js) require different backends. |
| 87 | 89 | ||
| 88 | In this example we make use for the first time of the `--pre-js` option of | 90 | In this example we make use time of the `--pre-js` option of the |
| 89 | the compiler to include custom JavaScript code that is run when our module | 91 | compiler to include custom JavaScript code that is run when our module |
| 90 | loads. Moreover, the C code is split into multiple files for convenience. | 92 | loads. Moreover, the C code is split into multiple files for convenience. |
