EmscriptenによるC/C++からWASMへのコンパイル
環境を構築し、Emscriptenツールチェーンを使って最初のC/C++プログラムをWebAssemblyモジュールにコンパイルします
「EmscriptenによるC/C++からWASMへのコンパイル」はCoddyKit上の無料WebAssembly (WASM) for High Performance Appsレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWebAssembly (WASM) for High Performance Apps学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 WebAssembly (WASM) for High Performance Appsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Meet Emscripten!
Welcome! Today, we'll learn to compile C/C++ code into WebAssembly (WASM). The key tool for this is Emscripten.
Emscripten is a complete toolchain that takes C/C++ code and compiles it into WASM, along with "glue" JavaScript code to help run it in a web browser or Node.js environment.
Bridging C/C++ to WASM
Why Emscripten? Browsers don't understand C/C++ directly. Emscripten acts as a powerful translator.
- It compiles your C/C++ into the efficient WASM binary format.
- It generates JavaScript "glue" code to load the WASM module and interact with it from JavaScript.
- It handles C/C++ standard library functions (like
printf) by providing JavaScript equivalents.
Setting Up Emscripten
Setting up Emscripten involves downloading the Emscripten SDK (emsdk). This SDK contains everything you need: the emcc compiler, Node.js, Python, and other utilities.
Typically, you'd run commands like:
git clone https://github.com/emscripten-core/emsdk.gitcd emsdk./emsdk install latest./emsdk activate latest
This prepares your environment to use emcc.
A Simple C Function
Let's start with a very simple C program. This program defines a function add and calls it from main. We'll compile this into WASM.
#include <stdio.h>
// A simple C function to add two numbers
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 7);
printf("Result of add(5, 7): %d\n", result);
return 0;
}The `emcc` Command
The heart of Emscripten is the emcc compiler. It's a drop-in replacement for gcc or clang that knows how to target WebAssembly.
You'll use emcc just like you would a standard C/C++ compiler, but with specific flags to control the WASM output.
Compiling Our C Code
Now, let's compile our add.c file. We'll tell emcc to output a JavaScript file that includes the compiled WASM module.
The command -o add.js tells Emscripten to create add.js (the glue code) and add.wasm (the binary module).
emcc add.c -o add.js -s EXPORTED_FUNCTIONS="['_add']" -s EXPORT_ES6=1The Generated Files
After running the emcc command, you'll find two main files:
add.wasm: This is the actual WebAssembly binary module. It contains the compiledaddfunction in a highly optimized, compact format.add.js: This is the JavaScript "glue" code. It's responsible for loadingadd.wasm, setting up the WASM environment, and providing an interface to call functions exported from the WASM module.
What the Glue Does
The generated JavaScript glue code (e.g., add.js) is crucial. It handles:
- Fetching and instantiating the
.wasmfile. - Setting up the WASM module's memory.
- Converting JavaScript types to WASM types and vice-versa.
- Providing an object (often named
Module) to interact with the WASM module's exported functions.
This allows you to seamlessly call your C functions from JavaScript.
Optimizing Your WASM
Emscripten allows you to optimize your WASM output for size and speed. This is crucial for web performance.
You can use optimization flags similar to gcc:
-O0: No optimizations (fastest compile time).-O1,-O2: Standard optimizations.-O3: Aggressive optimizations (slower compile, smaller/faster code).-Os: Optimize for size.-Oz: Optimize aggressively for smallest size.
For production, emcc -O3 or emcc -Oz are common choices.
Quick Check: Emscripten
You've learned about Emscripten and its role. Let's test your understanding!
Recap: Compiling C/C++
In this lesson, you've taken your first step into WebAssembly with C/C++!
- We introduced Emscripten as the toolchain for compiling C/C++ to WASM.
- You saw how
emcccompiles C code into.wasmand.jsglue files. - We discussed the purpose of both the WASM binary and the JavaScript glue code.
- You learned about basic optimization flags for
emcc.
Next, we'll dive into how to load and run these WASM modules in a web browser using JavaScript!
よくある質問
「EmscriptenによるC/C++からWASMへのコンパイル」レッスンは無料ですか?
はい。「EmscriptenによるC/C++からWASMへのコンパイル」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、WebAssembly (WASM) for High Performance Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 WebAssembly (WASM) for High Performance Appsコースには全4レッスンが含まれています。
「EmscriptenによるC/C++からWASMへのコンパイル」で何を学びますか?
環境を構築し、Emscriptenツールチェーンを使って最初のC/C++プログラムをWebAssemblyモジュールにコンパイルします ブラウザで直接実行するハンズオンコードでWebAssembly (WASM) for High Performance Appsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
WebAssembly (WASM) for High Performance Appsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのWebAssembly (WASM) for High Performance Appsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「EmscriptenによるC/C++からWASMへのコンパイル」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このWebAssembly (WASM) for High Performance Appsレッスンでコードを書いて実行できますか?
はい。すべてのWebAssembly (WASM) for High Performance Appsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- EmscriptenによるC/C++からWASMへのコンパイル
- JavaScriptでのWASMの読み込みと実行
- 基本的なデータ交換:プリミティブ型
- C/C++からJavaScript関数を呼び出す