【问题标题】:How do I embed v8 on Windows?如何在 Windows 上嵌入 v8?
【发布时间】:2021-07-06 13:26:26
【问题描述】:

如何在 Windows 上使用 Visual Studio 编译和嵌入 v8? The official guide 在 Linux 上工作,但不能在 Windows 上使用 Visual Studio,因为我在尝试构建 v8 和示例项目时不断收到编译错误。

【问题讨论】:

  • 可以使用 NuGet 包。

标签: c++ chromium v8


【解决方案1】:

v8 的构建过程似乎经常变化。那里有一些教程,但它们似乎都已经过时了。因此,在 Chromium 团队决定再次更改之前,这些步骤应该暂时有效。

获取 depot 工具和 v8 源代码

这些步骤复制自here

  1. 卸载 Python。我在 v8 编译器脚本找到错误的 Python 可执行文件而不是软件仓库工具中提供的那个时遇到问题。暂时卸载 Python,让您的生活更轻松。
  2. 配置 Visual Studio 和您的开发环境。您需要带有“使用 C++ 进行桌面开发”和“MFC/ATL 支持”组件的 VS2019。您将需要 Windows 10 SDK 和“Windows 调试工具”功能。
  3. 下载depot_tools bundle并将其解压到某个地方。

警告:不要从资源管理器中使用拖放或复制粘贴提取,这不会提取隐藏的“.git”文件夹,这是 depot_tools 自动更新所必需的。不过,您可以使用上下文菜单中的“全部提取...”。

  1. 将 depot_tools 添加到 PATH 的开头(必须在任何 Python 安装之前)。假设您将捆绑包解压缩到 C:\src\depot_tools,打开:

控制面板 → 系统和安全 → 系统 → 高级系统设置

如果您具有管理员权限,请修改 PATH 系统变量并将 C:\src\depot_tools 放在前面(或至少放在任何可能已经有 Python 或 Git 副本的目录前面)。

如果你没有管理员权限,你可以添加一个用户级的PATH环境变量,并把C:\src\depot_tools放在最前面,但是如果你的系统PATH里面有Python,那你就倒霉了。

  1. 另外,以同样的方式添加一个DEPOT_TOOLS_WIN_TOOLCHAIN系统变量,并将其设置为0。这告诉 depot_tools 使用您本地安装的 Visual Studio 版本(默认情况下,depot_tools 将尝试使用 google 内部版本)。

您可能还需要将变量 vs2017_installvs2019_install 设置为 Visual Studio 2017 或 19 的安装路径,例如 Visual Studio 2019 的 set vs2019_install=C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional

  1. 从 cmd.exe shell 运行命令 gclient。首次运行时,gclient 将安装使用代码所需的所有 Windows 特定位,包括 msysgit 和 python。
  • 如果您从非 cmd shell(例如 cygwin、PowerShell)运行 gclient,它可能看起来运行正常,但 msysgit、python 和其他工具可能无法正确安装。
  • 如果您在首次运行 gclient 时发现文件系统出现奇怪错误,您可能需要禁用 Windows 索引。
  1. 打开命令提示符并键入 where python 并确认 depot_tools python.bat 位于 python.exe 的任何副本之前。未能确保这一点可能会导致在使用 gn 时过度构建。

  2. App Execution Aliases 可能与系统上的其他 python 安装冲突,因此请通过打开控制面板的 App execution aliases 部分并取消选中这两个指向 App Installer 旁边的框来为 python.exepython3.exe 禁用这些.

  3. cd 到您要存储 v8 的目录并运行 mkdir v8 && cd v8 && fetch v8

  4. cd v8git checkout 9.0.257.19,这是撰写本文时的最新稳定版。

  5. 应用此补丁:

diff --git a/BUILD.gn b/BUILD.gn
index a9ab6783fa..ede2774d18 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1051,6 +1051,7 @@ config("toolchain") {
       "/wd4723",  # https://crbug.com/v8/7771
       "/wd4724",  # https://crbug.com/v8/7771
       "/wd4800",  # Forcing value to bool.
+      "/wd4805",
     ]
   }

这是为了消除有关将 bool 与 int 进行比较的警告。希望他们知道自己在做什么。

  1. 运行gclient sync 以获取所有依赖项。

编译 v8

  1. 运行python tools/dev/v8gen.py x64.debug

  2. v8\out.gn\x64.debug\args.gn的内容替换为:

is_debug = true
target_cpu = "x64"
v8_enable_backtrace = true
v8_enable_slow_dchecks = true
v8_optimized_debug = false
v8_monolithic = true
v8_use_external_startup_data = false
is_component_build = false
is_clang = false

  1. 运行ninja -C out.gn/x64.debug v8_monolith

  2. 运行python tools/dev/v8gen.py x64.release

  3. v8\out.gn\x64.release\args.gn的内容替换为:

is_debug = false
target_cpu = "x64"
is_component_build = false
v8_monolithic = true
v8_use_external_startup_data = false
is_component_build = false
is_clang = false

  1. 运行ninja -C out.gn/x64.release v8_monolith

你好世界

  1. 在 Visual Studio 中创建一个新的空 C++ 项目。

  2. 将库目录添加到项目中。

    1. 转到您的项目属性并将配置更改为所有配置,将平台更改为 x64。
    2. 在 VC++ 目录下,将v8\include 的绝对路径添加到包含目录。在此步骤之后和以下每个步骤之后单击应用。
    3. 将配置更改为调试并将v8\out.gn\x64.debug\obj 的绝对路径添加到库目录。
    4. 对发布配置重复步骤 3,路径为 v8\out.gn\x64.release\obj
    5. 更改为所有配置,转到链接器 |输入并将以下文件添加到 Additional Dependencies:v8_monolith.lib;dbghelp.lib;Winmm.lib;
    6. 转到 C/C++ |预处理器并将V8_COMPRESS_POINTERS;_ITERATOR_DEBUG_LEVEL=0; 添加到预处理器定义中。
    7. 更改为调试配置。转到 C/C++ |代码生成并将运行时库更改为多线程调试 (/MTd)。
    8. 更改为发布配置。将运行时库更改为多线程 (/MT)。
  3. 添加一个新的main.cpp 文件并添加以下代码,该代码是v8\samples\hello-world.cc 的略微修改版本。

// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "libplatform/libplatform.h"
#include "v8.h"

int main(int argc, char* argv[]) {
  // Initialize V8.
  v8::V8::InitializeICUDefaultLocation(argv[0]);
  v8::V8::InitializeExternalStartupData(argv[0]);
  std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
  v8::V8::InitializePlatform(platform.get());
  v8::V8::Initialize();

  // Create a new Isolate and make it the current one.
  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);

    // Create a stack-allocated handle scope.
    v8::HandleScope handle_scope(isolate);

    // Create a new context.
    v8::Local<v8::Context> context = v8::Context::New(isolate);

    // Enter the context for compiling and running the hello world script.
    v8::Context::Scope context_scope(context);

    {
      // Create a string containing the JavaScript source code.
      v8::Local<v8::String> source =
          v8::String::NewFromUtf8Literal(isolate, "'Hello' + ', World!'");

      // Compile the source code.
      v8::Local<v8::Script> script =
          v8::Script::Compile(context, source).ToLocalChecked();

      // Run the script to get the result.
      v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();

      // Convert the result to an UTF8 string and print it.
      v8::String::Utf8Value utf8(isolate, result);
      printf("%s\n", *utf8);
    }

    {
      // Use the JavaScript API to generate a WebAssembly module.
      //
      // |bytes| contains the binary format for the following module:
      //
      //     (func (export "add") (param i32 i32) (result i32)
      //       get_local 0
      //       get_local 1
      //       i32.add)
      //
      const char csource[] = R"(
        let bytes = new Uint8Array([
          0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01,
          0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07,
          0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01,
          0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b
        ]);
        let module = new WebAssembly.Module(bytes);
        let instance = new WebAssembly.Instance(module);
        instance.exports.add(3, 4);
      )";

      // Create a string containing the JavaScript source code.
      v8::Local<v8::String> source =
          v8::String::NewFromUtf8Literal(isolate, csource);

      // Compile the source code.
      v8::Local<v8::Script> script =
          v8::Script::Compile(context, source).ToLocalChecked();

      // Run the script to get the result.
      v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();

      // Convert the result to a uint32 and print it.
      uint32_t number = result->Uint32Value(context).ToChecked();
      printf("3 + 4 = %u\n", number);
    }
  }

  // Dispose the isolate and tear down V8.
  isolate->Dispose();
  v8::V8::Dispose();
  v8::V8::ShutdownPlatform();
  delete create_params.array_buffer_allocator;
  return 0;
}

  1. 编译并运行调试和发布配置。他们应该输出:
Hello, World!
3 + 4 = 7

【讨论】:

  • 这确实需要您的项目使用_ITERATOR_DEBUG_LEVEL=0 禁用选中的迭代器。没有它,您的项目将无法编译。您也不能在其他翻译单元启用它时为一个翻译单元禁用它。我无法修补 v8 代码以使用检查的迭代器。
  • 这个方法可以把JS转成C++吗?
  • @UahnbuTran 不,v8 允许您在 c++ 程序中运行 JS。如果你需要在 c++ 程序中运行 JS,v8 是最好的方法,除了重写 JS 代码。
  • 那么性能会有提升吗?
  • @UahnbuTran 与在 Chromium 浏览器中运行 JS 相比,它们在性能上是相同的。 Chromium 使用 v8。
猜你喜欢
  • 2011-08-22
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
相关资源
最近更新 更多