This document introduces some key V8 concepts and provides a hello world
example to get you started with V8 code.
This document is intended for C++ programmers who want to embed the V8 JavaScript engine within a C++ application.
Let's look at a Hello World example that takes a JavaScript statement as a string argument, executes it as JavaScript code, and prints the result to standard out.
First, some key concepts you will need:
These concepts are discussed in greater detail in the [[Embedder‘s Guide|Embedder’s Guide]].
Follow the steps below to run the example yourself:
git checkout refs/tags/7.1.11 -b sample -t
tools/dev/v8gen.py x64.release.sampleYou can inspect and manually edit the build configuration by running
gn args out.gn/x64.release.sample
ninja -C out.gn/x64.release.sample v8_monolith
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
cp out.gn/x64.release.sample/icudtl.dat .
hello_world
executable file at the command line. e.g. On Linux, in the V8 directory, run:./hello_world
Hello, World!
.Of course this is a very simple example and it‘s likely you’ll want to do more than just execute scripts as strings! For more information see the [[Embedder‘s Guide|Embedder’s Guide]]. If you are looking for an example which is in sync with master simply check out the file hello-world.cc
.