midrangemonroe
midrangemonroe

Reputation: 41

Building V8 on Windows does not output v8_base.lib

I want to build V8 and embed it in a C++ program to use SWIG to allow a Javascript app to call into the C++ library. However, after following the steps for building V8, I am missing some of the important libs for linking to V8 (e.g. V8_base.lib):

  1. fetch v8
  2. cd v8
  3. git pull origin
  4. gclient sync
  5. python tools/dev/v8gen.py x64.release
  6. ninja -C out.gn/x64.release

I have DEPOT_TOOLS_WIN_TOOLCHAIN = 0 and GYP_MSVS_VERSION = 2019. My args.gn is the following (I have built with and without the last line):

is_debug = false
target_cpu = "x64"
is_component_build = false
v8_static_library = true
is_clang = false
use_lld = false

After a successful build, I find that there is no v8_base.lib file under out.gn\x64.release\obj as all of the docs indicate there should be. Strangely, I see a v8.stamp and v8_base.stamp files, but no corresponding *.lib. What am I missing? Are these libraries no longer needed for embedding into a C++ program?

Upvotes: 4

Views: 1719

Answers (1)

wizebin
wizebin

Reputation: 730

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

  1. Python2 (Python3 For newer versions)
  2. Git
  3. Ninja
  4. G++ (or clang)

Updated instructions for building v8 (on linux/macos)

Here are the steps I followed today to successfully compile and run v8

  1. Install google's depot tools: git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" ./depot_tools
  2. Add the depot tools to your path export PATH=$(pwd)/depot_tools:$PATH
  3. If you are building on windows, be sure to set DEPOT_TOOLS_WIN_TOOLCHAIN=0 as an environment variable
  4. Download v8: fetch v8 OR fetch --no-history v8
  5. Move into the v8 directory cd v8
  6. Generate a release for your system (probably): tools/dev/v8gen.py x64.release
  7. 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)

  1. gn automatically regenerates build files when you close your editor
  2. 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;
}
  1. 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

Upvotes: 4

Related Questions