Basics
Building QuickJS
Start by cloning the repository
$ git clone https://github.com/bellard/quickjs/
Assuming you have GCC and Make installed, you can build the project by running make
inside the cloned repository.
$ cd quickjs
$ make
This will produce a few different components, including:
- libquickjs.a — This is the compiled static library which you can include in your C/C++ project.
- qjs — A command line Runtime and Read-Eval-Print-Loop (REPL) similar to Node built on QuickJS.
- qjsc — A compiler that can build an executable from JavaScript code. The compiled binary will contain both the engine and the compiled JavaScript bytecode.
To see how QuickJS handles different JavaScript features, you can run the REPL in the terminal and play around with it:
$ ./qjs
QuickJS - Type "\h" for help
qjs > console.log("Hello, world!")
Hello, world!
undefined
qjs > import (os);
Promise { }
qjs > os.sleepAsync(3000).then(() => console.log('Done sleeping'));
Promise { }
qjs > Done sleeping
Linking with QuickJS
All you need to use the engine in your C/C++ project is the quickjs.h
header file and the libquickjs.a
library. When compiling, include -Idirectory/containing/header
in your compiler flags, and include both -Ldirectory/containing/libquickjs
and -lquickjs
in your linker flags.
$ gcc -c -Iquickjs/ main.c
$ gcc -Lquickjs -lquickjs -o main main.o
$ ls
main main.c main.o quickjs