qjsc - QuickJS Compiler
Theqjsc executable is the QuickJS JavaScript compiler. It compiles JavaScript source files into bytecode or C code that can be embedded in executables.
Basic Usage
Output Modes
qjsc can generate output in three different formats:
- C Code (Default)
- Standalone Executable
- Raw Bytecode
Generate C code with embedded bytecode:This creates a C file containing bytecode as a byte array that can be compiled into your application.
Command-Line Options
Output Control
Set the output filename (default:
out.c)Output raw bytecode instead of C code
Output a complete C file with The generated C file can be compiled directly into a standalone executable.
main() function and bytecodeCode Generation
Set the C name of the generated data arrayBy default, the C name is derived from the filename.
Set the prefix for generated C names (default:
qjsc_)Set the script name as used in stack traces
Module Handling
Compile as ES6 module (default=autodetect)
Compile as classic JavaScript script (default=autodetect)
By default,
qjsc automatically detects whether a file is a module based on:- File extension (
.mjsfiles are modules) - Content analysis (presence of
import/exportstatements)
Compile a dynamically loaded module or worker
Add initialization code for an external C moduleThis adds initialization code for C modules that will be linked with the executable.
Do not add default system modules (Useful for creating minimal executables without standard library dependencies.
std, os, bjson)Optimization
Strip source code from the bytecode. Specify twice (Stripping reduces bytecode size but makes debugging harder.
-ss) to also strip debug info.Runtime Configuration
Set the maximum stack size to N bytes (default: 262144)
Help
Display help information and exit
Examples
Compile a Simple Script to C
hello.c contains:
Create a Standalone Executable
Compile Multiple Files
Compile with External C Module
Given a C modulefib.c with an js_init_module_fib function:
main.js:
Minimal Executable (No Standard Library)
Generate Optimized Bytecode
Compiling Modules
ES6 Module
Classic Script
Integration with Build Systems
Using with Make
Using with CMake
Generated C Code Structure
With -e flag (Standalone)
The generated C file includes:
Without -e flag (Library)
The generated C file includes only:
JS_ReadObject().
Compiling Full C Applications
When usingqjsc -e, you need to link against QuickJS and its dependencies:
The exact linker flags may vary depending on your system and QuickJS build configuration.
Common Use Cases
Embedding in Applications
Generate C code withoutmain() to embed in your own application:
Pre-compiling for Faster Startup
Compile to bytecode and load at runtime:See Also
- qjs - QuickJS interpreter for running JavaScript
- Creating Standalone Executables - Alternative method using qjs