Reputation: 41
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):
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
Reputation: 730
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
Here are the steps I followed today to successfully compile and run v8
git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" ./depot_tools
export PATH=$(pwd)/depot_tools:$PATH
DEPOT_TOOLS_WIN_TOOLCHAIN=0
as an environment variablefetch v8
OR fetch --no-history v8
cd v8
tools/dev/v8gen.py x64.release
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)
ninja -C out.gn/x64.release
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;
}
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