Overview
The instructions on the website to build a sample app are a bit better than whatever instructions you're following now
The v8_base.a
target was removed from the build.gn
file here in april, 2019
Prerequisites
- Python2 (Python3 For newer versions)
- Git
- Ninja
- G++ (or clang)
Updated instructions for building v8 (on linux/macos)
Here are the steps I followed today to successfully compile and run v8
- Install google's depot tools:
git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" ./depot_tools
- Add the depot tools to your path
export PATH=$(pwd)/depot_tools:$PATH
- If you are building on windows, be sure to set
DEPOT_TOOLS_WIN_TOOLCHAIN=0
as an environment variable
- Download v8:
fetch v8
OR fetch --no-history v8
- Move into the v8 directory
cd v8
- Generate a release for your system (probably):
tools/dev/v8gen.py x64.release
- Run
gn args out.gn/x64.release
to the following build arguments:
is_debug = false
target_cpu = "x64"
use_custom_libcxx = false
v8_monolithic = true
v8_use_external_startup_data = false
is_clang = {whether or not you're using clang}
(* More here)
- gn automatically regenerates build files when you close your editor
- Build v8 (slow)
ninja -C out.gn/x64.release
Using your newly created v8 library
In the root of your project directory create a folder called src
and inside that folder create a new file main.cc
which looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/libplatform/libplatform.h"
#include "include/v8.h"
int main(int argc, char* argv[]) {
v8::V8::InitializeICUDefaultLocation(argv[0]);
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate); // Create a stack-allocated handle scope.
v8::Local<v8::Context> context = v8::Context::New(isolate); // Create a new context.
v8::Context::Scope context_scope(context); // Enter the context for compiling and running the hello world script.
{
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, "Object.keys({ h: 1, e: 2, ll: 3, o: 4, _: 5, w: 6, or: 7, l: 8, d: 9 })", v8::NewStringType::kNormal).ToLocalChecked(); // Create a string containing the JavaScript source code.
v8::Local<v8::Script> script = v8::Script::Compile(context, source).ToLocalChecked(); // Compile the source code.
v8::Local<v8::Value> result = script->Run(context).ToLocalChecked(); // Run the script to get the result.
v8::String::Utf8Value utf8(isolate, result); // Convert the result to an UTF8 string and print it.
printf("%s\n", *utf8);
}
}
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete create_params.array_buffer_allocator;
return 0;
}
- Build your source using the new monolithic library created in the ninja build:
g++ -Iv8/include src/main.cc -o bin/my_program -lv8_monolith -Lv8/out.gn/x64.release.sample/obj/ -pthread -std=c++11
For this to work, your file structure should look something like
depot_tools
v8
src/main.cc
It will create a new executable in your bin
folder called my_program
which you can execute directly ./bin/my_program