我想构建V8并将其嵌入C ++程序中,以使用SWIG允许Javascript应用程序调用C ++库。但是,按照构建V8的步骤操作后,我缺少了一些重要的库以链接到V8(例如V8_base.lib):
我有DEPOT_TOOLS_WIN_TOOLCHAIN = 0且GYP_MSVS_VERSION = 2019。我的args.gn是以下内容(我在有无最后一行的情况下构建):
is_debug = false
target_cpu = "x64"
is_component_build = false
v8_static_library = true
is_clang = false
use_lld = false
在成功构建之后,我发现out.gn \ x64.release \ obj下没有v8_base.lib文件,因为所有文档都表明应该存在。奇怪的是,我看到一个v8.stamp和v8_base.stamp文件,但是没有相应的* .lib。我想念什么?不再需要将这些库嵌入到C ++程序中吗?
[The instructions on the website to build a sample app比您现在遵循的说明要好一些]
v8_base.a
目标已于2019年4月从build.gn
文件here中删除
这里是我今天成功编译和运行v8的步骤
git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" ./depot_tools
fetch v8
或fetch --no-history v8
cd v8
tools/dev/v8gen.py x64.release
out.gn/x64.release/args.gn
以包含以下文本:is_debug = false
target_cpu = "x64"
use_custom_libcxx = false
v8_monolithic = true
v8_use_external_startup_data = false
gn gen out.gn/x64.release
ninja -C out.gn/x64.release
在项目目录的根目录中创建一个名为src
的文件夹,并在该文件夹中创建一个新文件main.cc
,如下所示:
#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 -Iv8/include src/main.cc -o bin/my_program -lv8_monolith -Lv8/out.gn/x64.release.sample/obj/ -pthread -std=c++0x
为此,您的文件结构应类似于
depot_tools
v8
src/main.cc
它将在您的bin
文件夹中创建一个名为my_program
的新可执行文件,您可以直接执行./bin/my_program