C/C++
Build WebAssembly applications with C/C++ using Emscripten.
Overview
C/C++ has excellent WebAssembly support through Emscripten. Wasmrun includes built-in support for C/C++ projects without requiring a separate plugin.
Prerequisites
- Emscripten SDK (install from emscripten.org)
- wasmrun installed (see Installation)
Quick Start
# Create project
mkdir my-c-wasm && cd my-c-wasm
# Create hello.c
cat > hello.c << 'EOF'
#include <stdio.h>
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int add(int a, int b) {
return a + b;
}
EMSCRIPTEN_KEEPALIVE
void greet(const char* name) {
printf("Hello, %s!\n", name);
}
int main() {
printf("C WebAssembly module loaded!\n");
return 0;
}
EOF
# Create Makefile
cat > Makefile << 'EOF'
CC = emcc
CFLAGS = -O2
all:
$(CC) $(CFLAGS) hello.c -o hello.html
clean:
rm -f hello.wasm hello.js hello.html
EOF
# Run with Wasmrun
wasmrun run .
Compiling
Basic Compilation
emcc hello.c -o hello.html
Optimization
# Optimize for size
emcc -Os hello.c -o hello.wasm
# Optimize for speed
emcc -O3 hello.c -o hello.wasm
WASI Target
emcc --target=wasm32-wasi hello.c -o hello.wasm
Example Project
See examples/c-hello for a complete example.
Best Practices
- Use
EMSCRIPTEN_KEEPALIVEto export functions - Enable optimizations for production
- Minimize memory usage
- Use
printffor console output