Skip to main content

Embedding QuickJS in C

This tutorial demonstrates how to compile JavaScript code and embed it in a standalone C executable.
1

Create a JavaScript file

First, create a simple JavaScript file hello.js:
console.log("Hello World");
2

Compile to C code

Use qjsc to compile the JavaScript file into C code:
qjsc -e -o hello.c hello.js
The -e flag tells qjsc to generate a main() function and embed the bytecode in the C file.This generates hello.c containing:
  • Compiled JavaScript bytecode as a C array
  • A main() function that initializes QuickJS and executes the bytecode
3

Compile the C executable

Compile the generated C code with the QuickJS library:
cc hello.c dtoa.c libregexp.c libunicode.c quickjs.c quickjs-libc.c -I. -o hello
Required source files:
  • hello.c - Your generated code
  • dtoa.c - Double-to-ASCII conversion
  • libregexp.c - Regular expression library
  • libunicode.c - Unicode support
  • quickjs.c - Core QuickJS engine
  • quickjs-libc.c - Standard library
4

Run the executable

Execute the standalone binary:
./hello
Expected output:
Hello World
The resulting binary is completely self-contained and includes the QuickJS runtime.

Alternative: Create Standalone Executables with qjs

QuickJS also provides a simpler method to create standalone executables:
qjs -c hello.js -o hello --exe qjs
This bundles the JavaScript file directly into a copy of the qjs executable without requiring manual C compilation.

Compiler Options

Useful qjsc options:
  • -e - Output main() and bytecode in C file
  • -b - Output raw bytecode instead of C code
  • -o output - Set output filename
  • -n name - Set script name (used in stack traces)
  • -s - Strip source code (use twice to strip debug info)
  • -S n - Set maximum stack size in bytes (default: 262144)

Next Steps

Build docs developers (and LLMs) love