Embedding QuickJS in C
This tutorial demonstrates how to compile JavaScript code and embed it in a standalone C executable.Compile to C code
Use The
qjsc to compile the JavaScript file into C code:-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
Compile the C executable
Compile the generated C code with the QuickJS library:Required source files:
hello.c- Your generated codedtoa.c- Double-to-ASCII conversionlibregexp.c- Regular expression librarylibunicode.c- Unicode supportquickjs.c- Core QuickJS enginequickjs-libc.c- Standard library
Alternative: Create Standalone Executables with qjs
QuickJS also provides a simpler method to create standalone executables:qjs executable without requiring manual C compilation.
Compiler Options
Usefulqjsc 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
- Learn about native C functions
- Explore bytecode compilation
- Work with JavaScript modules