The V8 API provides functions for compiling and executing scripts, accessing C++ methods and data structures, handling errors, and enabling security checks. Your application can use V8 just like any other C++ library. Your C++ code accesses V8 through the V8 API by including the header
Handles and garbage collection
Accessing dynamic variables
Guide to embedding V8
Follow the steps below to run the example yourself:
- Download the V8 source code by following the Git instructions.
- The instructions for this hello world example have last been tested with V8 v7.1.11. You can check out this branch with git checkout refs/tags/7.1.11 -b sample -t
- Create a build configuration using the helper script:
You can inspect and manually edit the build configuration by running:
gn args out.gn/x64.release.sample
- Build the static library on a Linux 64 system:
ninja -C out.gn/x64.release.sample v8_monolith
- Compile hello-world.cc, linking to the static library created in the build process. For example, on 64bit Linux using the GNU compiler:
g++ -I. -Iinclude samples/hello-world.cc -o hello_world -lv8_monolith -Lout.gn/x64.release.sample/obj/ -pthread -std=c++0x -DV8_COMPRESS_POINTERS
- For more complex code, V8 fails without an ICU data file. Copy this file to where your binary is stored:
cp out.gn/x64.release.sample/icudtl.dat .
- Run the hello_world executable file at the command line. e.g. On Linux, in the V8 directory, run:
- It prints Hello, World!. Yay!
If you are looking for an example which is in sync with master, check out the file hello-world.cc. This is a very simple example and you’ll likely want to do more than just execute scripts as strings.