Emscripten으로 C/C++를 WASM으로 컴파일하기
개발 환경을 설정하고 Emscripten 도구 모음을 사용하여 첫 C/C++ 프로그램을 WebAssembly 모듈로 컴파일합니다.
Emscripten으로 C/C++를 WASM으로 컴파일하기은(는) CoddyKit의 무료 WebAssembly (WASM) for High Performance Apps 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 WebAssembly (WASM) for High Performance Apps 강의 전체를 잠금 해제할 수 있습니다. WebAssembly (WASM) for High Performance Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
“Emscripten으로 C/C++를 WASM으로 컴파일하기”에서 뭘 배우나요?
개발 환경을 설정하고 Emscripten 도구 모음을 사용하여 첫 C/C++ 프로그램을 WebAssembly 모듈로 컴파일합니다. 브라우저에서 직접 실행하는 실습 코드로 WebAssembly (WASM) for High Performance Apps을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
WebAssembly (WASM) for High Performance Apps을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 WebAssembly (WASM) for High Performance Apps은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“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 함수 호출하기