{"id":2103,"date":"2024-07-10T21:18:56","date_gmt":"2024-07-10T21:18:56","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=2103"},"modified":"2024-07-10T21:19:00","modified_gmt":"2024-07-10T21:19:00","slug":"how-to-use-the-cpp-emscripten-toolchain-for-webassembly","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/","title":{"rendered":"How to Use the C++ Emscripten Toolchain for WebAssembly"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. It is designed as a portable target for compilation of high-level languages like C++, enabling deployment on the web for client and server applications. Emscripten is an open-source compiler toolchain that allows you to compile C++ code into WebAssembly. This tutorial will guide you through the process of using the Emscripten toolchain to compile C++ code into WebAssembly and run it in a web environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving into the tutorial, ensure you have the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Basic understanding of C++.<\/li>\n\n\n\n<li>Familiarity with web development (HTML, JavaScript).<\/li>\n\n\n\n<li>Installed Node.js and npm.<\/li>\n\n\n\n<li>Installed Python (Emscripten uses Python scripts).<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Emscripten<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To start using Emscripten, you first need to install it. Emscripten requires several dependencies, which can be easily managed using its SDK (emsdk).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Clone the Emscripten SDK<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, clone the emsdk repository:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">git clone https:\/\/github.com\/emscripten-core\/emsdk.git\ncd emsdk<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 2: Install and Activate the Latest SDK<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, install the latest SDK and activate it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">.\/emsdk install latest\n.\/emsdk activate latest<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 3: Set Up Environment Variables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Set up the environment variables to make the emsdk tools available in your terminal session:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">source .\/emsdk_env.sh<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">You might want to add the above line to your <code>.bashrc<\/code> or <code>.zshrc<\/code> file to set up the environment variables automatically for every session.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Compiling C++ Code to WebAssembly<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Writing a Simple C++ Program<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start with a simple C++ program. Create a file named <code>hello.cpp<\/code> with the following content:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;iostream&gt;<\/span><\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Hello, WebAssembly!\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 2: Compiling with Emscripten<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To compile the C++ code to WebAssembly, use the <code>emcc<\/code> compiler provided by Emscripten. Run the following command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">emcc hello.cpp -o hello.html<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This command tells Emscripten to compile <code>hello.cpp<\/code> and generate an HTML file named <code>hello.html<\/code> along with the corresponding WebAssembly and JavaScript files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Understanding the Output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Emscripten generates several files:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>hello.html<\/code> &#8211; An HTML file to load and run the WebAssembly module.<\/li>\n\n\n\n<li><code>hello.js<\/code> &#8211; A JavaScript file that loads the WebAssembly module.<\/li>\n\n\n\n<li><code>hello.wasm<\/code> &#8211; The WebAssembly binary file.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">You can open <code>hello.html<\/code> in a web browser to see the output of your C++ program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Integrating WebAssembly with JavaScript<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">While running C++ code in a browser is interesting, the true power of WebAssembly comes from its ability to interact with JavaScript. Let&#8217;s modify our example to demonstrate this interaction.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Modifying the C++ Program<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Update <code>hello.cpp<\/code> to expose a function to JavaScript:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;emscripten.h&gt;<\/span><\/span>\n\n<span class=\"hljs-keyword\">extern<\/span> <span class=\"hljs-string\">\"C\"<\/span> {\n    <span class=\"hljs-function\">EMSCRIPTEN_KEEPALIVE\n    <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">sayHello<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Hello from WebAssembly!\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The <code>EMSCRIPTEN_KEEPALIVE<\/code> macro ensures that the <code>sayHello<\/code> function is not removed by Emscripten&#8217;s optimizer. The <code>extern \"C\"<\/code> block prevents C++ name mangling, making the function name predictable in JavaScript.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Compiling with Emscripten<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Compile the updated C++ code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">emcc hello.cpp -o hello.js -s EXPORTED_FUNCTIONS='&#91;\"_sayHello\"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='&#91;\"ccall\", \"cwrap\"]'<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The <code>-s EXPORTED_FUNCTIONS<\/code> option specifies the functions to be exposed to JavaScript. The <code>-s EXTRA_EXPORTED_RUNTIME_METHODS<\/code> option includes additional runtime methods that facilitate interaction between JavaScript and WebAssembly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Writing JavaScript to Call WebAssembly Functions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create an HTML file named <code>index.html<\/code> with the following content:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-meta-keyword\">html<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>WebAssembly with Emscripten<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>WebAssembly with Emscripten<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"sayHelloButton\"<\/span>&gt;<\/span>Say Hello<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"hello.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span>&gt;<\/span><span class=\"javascript\">\n        <span class=\"hljs-keyword\">var<\/span> sayHelloButton = <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">\"sayHelloButton\"<\/span>);\n        sayHelloButton.addEventListener(<span class=\"hljs-string\">\"click\"<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n            Module.ccall(<span class=\"hljs-string\">\"sayHello\"<\/span>, <span class=\"hljs-literal\">null<\/span>, &#91;], &#91;]);\n        });\n    <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 4: Running the Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Serve the <code>index.html<\/code> file using a local web server. You can use a simple Python HTTP server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">python -m http.server<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Open your browser and navigate to <code>http:\/\/localhost:8000<\/code>. Click the &#8220;Say Hello&#8221; button to see the interaction between JavaScript and WebAssembly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced Emscripten Features<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">File System Integration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Emscripten provides a virtual file system that allows your WebAssembly code to read and write files. This can be useful for scenarios like processing user-uploaded files or saving data locally.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Modifying the C++ Program<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Update <code>hello.cpp<\/code> to demonstrate file system usage:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;emscripten.h&gt;<\/span><\/span>\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;stdio.h&gt;<\/span><\/span>\n\n<span class=\"hljs-keyword\">extern<\/span> <span class=\"hljs-string\">\"C\"<\/span> {\n    <span class=\"hljs-function\">EMSCRIPTEN_KEEPALIVE\n    <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">writeFile<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        FILE* file = fopen(<span class=\"hljs-string\">\"\/hello.txt\"<\/span>, <span class=\"hljs-string\">\"w\"<\/span>);\n        <span class=\"hljs-keyword\">if<\/span> (file) {\n            <span class=\"hljs-built_in\">fputs<\/span>(<span class=\"hljs-string\">\"Hello, File System!\"<\/span>, file);\n            fclose(file);\n        }\n    }\n\n    <span class=\"hljs-function\">EMSCRIPTEN_KEEPALIVE\n    <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">readFile<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        FILE* file = fopen(<span class=\"hljs-string\">\"\/hello.txt\"<\/span>, <span class=\"hljs-string\">\"r\"<\/span>);\n        <span class=\"hljs-keyword\">if<\/span> (file) {\n            <span class=\"hljs-keyword\">char<\/span> buffer&#91;<span class=\"hljs-number\">100<\/span>];\n            fgets(buffer, <span class=\"hljs-keyword\">sizeof<\/span>(buffer), file);\n            fclose(file);\n            <span class=\"hljs-built_in\">printf<\/span>(<span class=\"hljs-string\">\"File content: %s\\n\"<\/span>, buffer);\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 2: Compiling with Emscripten<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Compile the updated C++ code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">emcc hello.cpp -o hello.js -s EXPORTED_FUNCTIONS='&#91;\"_writeFile\", \"_readFile\"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='&#91;\"ccall\", \"cwrap\"]' --preload-file hello.txt<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The <code>--preload-file<\/code> option bundles the specified file with the WebAssembly module.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Writing JavaScript to Call File System Functions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Update <code>index.html<\/code> to include buttons for writing and reading the file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-meta-keyword\">html<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>WebAssembly with Emscripten<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>WebAssembly with Emscripten<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"writeFileButton\"<\/span>&gt;<\/span>Write File<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"readFileButton\"<\/span>&gt;<\/span>Read File<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"hello.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span>&gt;<\/span><span class=\"javascript\">\n        <span class=\"hljs-keyword\">var<\/span> writeFileButton = <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">\"writeFileButton\"<\/span>);\n        writeFileButton.addEventListener(<span class=\"hljs-string\">\"click\"<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n            Module.ccall(<span class=\"hljs-string\">\"writeFile\"<\/span>, <span class=\"hljs-literal\">null<\/span>, &#91;], &#91;]);\n        });\n\n        <span class=\"hljs-keyword\">var<\/span> readFileButton = <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">\"readFileButton\"<\/span>);\n        readFileButton.addEventListener(<span class=\"hljs-string\">\"click\"<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n            Module.ccall(<span class=\"hljs-string\">\"readFile\"<\/span>, <span class=\"hljs-literal\">null<\/span>, &#91;], &#91;]);\n        });\n    <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 4: Running the Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Serve the <code>index.html<\/code> file using a local web server and open it in your browser. Click the &#8220;Write File&#8221; and &#8220;Read File&#8221; buttons to see file system operations in action.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using C++ Libraries with Emscripten<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Emscripten supports many popular C++ libraries, allowing you to leverage existing codebases in your WebAssembly projects. In this section, we&#8217;ll demonstrate how to use the SDL2 library with Emscripten.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Installing SDL2<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Emscripten includes precompiled versions of several libraries, including SDL2. You can install SDL2 using emsdk:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">.\/emsdk install sdl2\n.\/emsdk activate sdl2<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 2: Writing an SDL2 Program<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a file named <code>sdl_example.cpp<\/code> with the following content:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;SDL.h&gt;<\/span><\/span>\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;emscripten.h&gt;<\/span><\/span>\n\nSDL_Window* window;\nSDL_Renderer* renderer;\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main_loop<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    SDL_Event event;\n    <span class=\"hljs-keyword\">while<\/span> (SDL_PollEvent(&amp;event)) {\n        <span class=\"hljs-keyword\">if<\/span> (event.type == SDL_QUIT) {\n            emscripten_cancel_main_loop();\n            <span class=\"hljs-keyword\">return<\/span>;\n        }\n    }\n\n    SDL_SetRenderDrawColor(renderer, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">255<\/span>);\n    SDL_RenderClear(renderer);\n\n    SDL_SetRenderDrawColor(renderer, <span class=\"hljs-number\">255<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">255<\/span>);\n    SDL_RenderPresent(renderer);\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    SDL_Init(SDL_INIT_VIDEO);\n    window = SDL_CreateWindow(<span class=\"hljs-string\">\"SDL2 Emscripten Example\"<\/span>, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, <span class=\"hljs-number\">800<\/span>, <span class=\"hljs-number\">600<\/span>, SDL_WINDOW_SHOWN);\n    renderer = SDL_CreateRenderer(window, <span class=\"hljs-number\">-1<\/span>, <span class=\"hljs-number\">0<\/span>);\n\n    <span class=\"hljs-function\">em\n\n<span class=\"hljs-title\">scripten_set_main_loop<\/span><span class=\"hljs-params\">(main_loop, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>)<\/span><\/span>;\n\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 3: Compiling with Emscripten<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Compile the SDL2 program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">emcc sdl_example.cpp -o sdl_example.html -s USE_SDL=2<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The <code>-s USE_SDL=2<\/code> option tells Emscripten to link against the SDL2 library.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Running the Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Serve the <code>sdl_example.html<\/code> file using a local web server and open it in your browser. You should see a window with a black background and a red rectangle.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Debugging WebAssembly<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Debugging WebAssembly can be challenging due to its binary nature. However, Emscripten provides several tools and techniques to make debugging easier.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Source Maps<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Emscripten can generate source maps to map the WebAssembly code back to the original C++ source code. This allows you to use browser developer tools to debug your C++ code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Compiling with Source Maps<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Compile your C++ code with the <code>-g<\/code> option to generate source maps:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">emcc hello.cpp -o hello.js -g<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 2: Using Browser Developer Tools<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open the HTML file in your browser and open the developer tools (usually by pressing F12). In the &#8220;Sources&#8221; tab, you should see the original C++ source files. You can set breakpoints, step through code, and inspect variables just like you would with JavaScript.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Logging and Console Output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can use standard C++ functions like <code>printf<\/code> and <code>std::cout<\/code> to log messages to the browser console. This can be useful for debugging and understanding the flow of your program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Optimizing WebAssembly<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Emscripten provides several optimization options to reduce the size and improve the performance of your WebAssembly modules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Compiling with Optimizations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the <code>-O<\/code> options to enable optimizations. For example, <code>-O2<\/code> enables moderate optimizations, while <code>-O3<\/code> enables aggressive optimizations:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">emcc hello.cpp -o hello.js -O2<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 2: Using Link-Time Optimization<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Link-Time Optimization (LTO) can further reduce the size of your WebAssembly modules by performing optimizations across all compilation units. Use the <code>-flto<\/code> option to enable LTO:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">emcc hello.cpp -o hello.js -O2 -flto<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 3: Minifying JavaScript<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Emscripten can minify the generated JavaScript code to reduce its size. Use the <code>--closure 1<\/code> option to enable Closure Compiler:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">emcc hello.cpp -o hello.js -O2 --closure 1<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we covered the basics of using the Emscripten toolchain to compile C++ code to WebAssembly. We explored various features, including integrating WebAssembly with JavaScript, using the virtual file system, leveraging existing C++ libraries, debugging, and optimizing WebAssembly modules. With these skills, you can start building high-performance web applications using C++ and WebAssembly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">WebAssembly is a powerful technology that enables you to run C++ code in the browser with near-native performance. Emscripten makes it easy to compile your existing C++ codebases to WebAssembly and integrate them with modern web technologies. As you continue to explore WebAssembly and Emscripten, you&#8217;ll discover even more possibilities for creating fast, efficient, and feature-rich web applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Additional Resources<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/emscripten.org\/docs\/\" target=\"_blank\" rel=\"noreferrer noopener\">Emscripten Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/webassembly.org\/docs\/\" target=\"_blank\" rel=\"noreferrer noopener\">WebAssembly Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/WebAssembly\" target=\"_blank\" rel=\"noreferrer noopener\">MDN Web Docs: Using WebAssembly<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/emscripten-core\/emscripten\" target=\"_blank\" rel=\"noreferrer noopener\">Emscripten GitHub Repository<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">By following this tutorial and exploring the additional resources, you&#8217;ll gain a deeper understanding of WebAssembly and how to effectively use Emscripten to bring your C++ applications to the web. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. It is designed as a portable target for compilation of high-level languages like C++, enabling deployment on the web for client and server applications. Emscripten is an open-source compiler toolchain that allows you to compile C++ code into WebAssembly. This tutorial will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9,4],"tags":[],"class_list":["post-2103","post","type-post","status-publish","format-standard","category-cplusplus","category-programming-languages","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Use the C++ Emscripten Toolchain for WebAssembly<\/title>\n<meta name=\"description\" content=\"WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. It is designed as a portable target for compilation of high-level languages\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use the C++ Emscripten Toolchain for WebAssembly\" \/>\n<meta property=\"og:description\" content=\"WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. It is designed as a portable target for compilation of high-level languages\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-10T21:18:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-10T21:19:00+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"How to Use the C++ Emscripten Toolchain for WebAssembly\",\"datePublished\":\"2024-07-10T21:18:56+00:00\",\"dateModified\":\"2024-07-10T21:19:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\\\/\"},\"wordCount\":1121,\"articleSection\":[\"C++\",\"Programming Languages\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\\\/\",\"name\":\"How to Use the C++ Emscripten Toolchain for WebAssembly\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2024-07-10T21:18:56+00:00\",\"dateModified\":\"2024-07-10T21:19:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. It is designed as a portable target for compilation of high-level languages\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Use the C++ Emscripten Toolchain for WebAssembly\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use the C++ Emscripten Toolchain for WebAssembly","description":"WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. It is designed as a portable target for compilation of high-level languages","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/","og_locale":"en_US","og_type":"article","og_title":"How to Use the C++ Emscripten Toolchain for WebAssembly","og_description":"WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. It is designed as a portable target for compilation of high-level languages","og_url":"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/","article_published_time":"2024-07-10T21:18:56+00:00","article_modified_time":"2024-07-10T21:19:00+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"How to Use the C++ Emscripten Toolchain for WebAssembly","datePublished":"2024-07-10T21:18:56+00:00","dateModified":"2024-07-10T21:19:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/"},"wordCount":1121,"articleSection":["C++","Programming Languages"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/","url":"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/","name":"How to Use the C++ Emscripten Toolchain for WebAssembly","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2024-07-10T21:18:56+00:00","dateModified":"2024-07-10T21:19:00+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. It is designed as a portable target for compilation of high-level languages","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/how-to-use-the-cpp-emscripten-toolchain-for-webassembly\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"How to Use the C++ Emscripten Toolchain for WebAssembly"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=2103"}],"version-history":[{"count":1,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2103\/revisions"}],"predecessor-version":[{"id":2104,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2103\/revisions\/2104"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=2103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=2103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=2103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}