C++ Google V8库 源码编译及使用(VS2019)

必须要有代理

准备

配置临时环境变量

管理员身份运行控制台(cmd)

set HTTP_PROXY=http://127.0.0.1:10809
set HTTPS_PROXY=http://127.0.0.1:10809
set DEPOT_TOOLS_WIN_TOOLCHAIN=0

代理端口查看设置里面的

C++ Google V8库 源码编译及使用(VS2019)_第1张图片

因为是临时环境变量,每次打开控制台都需要重新设置。

下载 depot tools

https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html#_setting_up

 点击 budle 下载后解压

C++ Google V8库 源码编译及使用(VS2019)_第2张图片

 将该目录加入到环境变量path中

 执行 gclient

 出现这个提示表示下载完成。 

安装windows sdk10

直接在vs中安装就行了

C++ Google V8库 源码编译及使用(VS2019)_第3张图片

下载源码

fetch v8

C++ Google V8库 源码编译及使用(VS2019)_第4张图片

如果中途失败,继续执行 gclient sync

 出现这个提示表示下载完成。

关于路径问题

如果VS的安装目录和windows sdk的安装目录都是默认在C盘,可以跳过。

这里会出现找不到 VS 的错误。

C++ Google V8库 源码编译及使用(VS2019)_第5张图片

编辑文件 build/vs_toolchain.py 定位错误位置

C++ Google V8库 源码编译及使用(VS2019)_第6张图片

分析后发现,path = os.environ.get('vs%s_install' % version) 这个是通过获取环境变量获取VS的安装类路径。直接设置环境变量

set "vs2019_install=F:\Microsoft Visual Studio\2019"

继续报错

C++ Google V8库 源码编译及使用(VS2019)_第7张图片

 打开文件 build\toolchain\win\setup_toolchain.py

C++ Google V8库 源码编译及使用(VS2019)_第8张图片

 还是缺少环境变量

set "GYP_MSVS_OVERRIDE_PATH=F:\Microsoft Visual Studio\2019\Community"

编译 

使用 clang libc++ 编译动态库

根据需要编译的平台选择相应的控制台,64位的选择x64,32位的选择x86

C++ Google V8库 源码编译及使用(VS2019)_第9张图片

因为是新开控制台,需要重新设置 

set DEPOT_TOOLS_WIN_TOOLCHAIN=0

设置 gn 参数

gn args out.cl/x64.debug
is_debug=true 
target_cpu="x64"
v8_enable_i18n_support=false 
v8_use_external_startup_data=false 
is_component_build=true

C++ Google V8库 源码编译及使用(VS2019)_第10张图片

可以生成 vs 解决方案

gn gen --ide=vs out.cl\x64.debug

可以直接打开vs编译,也可以继续命令行编译

C++ Google V8库 源码编译及使用(VS2019)_第11张图片

C++ Google V8库 源码编译及使用(VS2019)_第12张图片

 按照网上说法直接编译 gn_all

或者命令行编译

ninja -C out.cl/x64.debug v8

 C++ Google V8库 源码编译及使用(VS2019)_第13张图片

 打开解决方案

C++ Google V8库 源码编译及使用(VS2019)_第14张图片

 查看v8_hello_world的代码

// 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 
#include 
#include 

#include "include/libplatform/libplatform.h"
#include "include/v8-context.h"
#include "include/v8-initialization.h"
#include "include/v8-isolate.h"
#include "include/v8-local-handle.h"
#include "include/v8-primitive.h"
#include "include/v8-script.h"

int main(int argc, char* argv[]) {
  // Initialize V8.
  v8::V8::InitializeICUDefaultLocation(argv[0]);
  v8::V8::InitializeExternalStartupData(argv[0]);
  std::unique_ptr 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 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 source =
          v8::String::NewFromUtf8Literal(isolate, "'Hello' + ', World!'");

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

      // Run the script to get the result.
      v8::Local 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 source =
          v8::String::NewFromUtf8Literal(isolate, csource);

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

      // Run the script to get the result.
      v8::Local 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;
}

 运行结果:

C++ Google V8库 源码编译及使用(VS2019)_第15张图片

如果用vs创建项目使用v8库,会报链接错误 unresolved external symbol  v8::platform::NewDefaultPlatform:

通过查看v8_libplatform.dll.lib的导出符号

C++ Google V8库 源码编译及使用(VS2019)_第16张图片

发现: 

libc++编译的标准库会出现std::__1::这种命名空间,而libstdc++则是std::。

参考这里的说法:

VS2017链接V8引擎库遇到的一些问题_myctime的博客-CSDN博客

是因为libc++的库在实现STL的时候使用了inline namespace __1,而libstdc++则没有。

按照上面的解决方法,我没有成功。转而采用下面的方法。

不使用 clang 编译静态库

设置 gn 参数

gn args out/x64.release
v8_monolithic=true 
v8_use_external_startup_data=false 
use_custom_libcxx=false 
is_component_build=false 
treat_warnings_as_errors=false 
v8_symbol_level=0 
is_clang=false 
is_debug=false

如果需要编译debug版本,设置is_debug=true

顺便生成 vs 解决方案

gn gen --ide=vs out\x64.release

命令行编译

ninja -C out/x64.release v8_monolith

C++ Google V8库 源码编译及使用(VS2019)_第17张图片

测试执行js代码(包括调用js方法):

创建一个控制台应用,如下设置

C++ Google V8库 源码编译及使用(VS2019)_第18张图片

C++ Google V8库 源码编译及使用(VS2019)_第19张图片

 

C++ Google V8库 源码编译及使用(VS2019)_第20张图片

C++ Google V8库 源码编译及使用(VS2019)_第21张图片

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

#ifndef _DEBUG
#ifdef _WIN64
#pragma comment (lib,"lib/x64/Release/v8_monolith.lib")
#else
#endif // _WIN64
#else
#ifdef _WIN64
#pragma comment (lib,"lib/x64/Debug/v8_monolith.lib")
#else
#endif // _WIN64
#endif


int main(int argc, char* argv[]) {
    // Initialize V8.
    char* s = NULL;
    _get_pgmptr(&s);
    std::string path(s);
    size_t i = path.find_last_of("\\");
    path = path.substr(0, i);

    v8::V8::InitializeICUDefaultLocation(path.c_str());
    v8::V8::InitializeExternalStartupData(path.c_str());
    std::unique_ptr 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 context = v8::Context::New(isolate);

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

        /*
        i = path.find_last_of("\\");
        path = path.substr(0, i);
        i = path.find_last_of("\\");
        path = path.substr(0, i);
        std::string js = path + "\\t.js";

        FILE* file;
        fopen_s(&file, js.c_str(), "rb");
        if (file != NULL) {
            fseek(file, 0, SEEK_END);
            size_t len = ftell(file);
            rewind(file);
            std::unique_ptr buf(new char[len + 1]);
            buf.get()[len] = '\0';
            for (size_t i = 0; i < len;) {
                i += fread(&buf.get()[i], 1, len - i, file);
            }
            fclose(file);
        }
        */
        std::string buf = "(function(){return \"Hello\";})();";

        // Create a string containing the JavaScript source code.
        v8::Local source =
            v8::String::NewFromUtf8(isolate, buf.c_str(), v8::NewStringType::kNormal, static_cast(buf.length())).ToLocalChecked();

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

        // Run the script to get the result.
        v8::Local 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);
    }

    {
        v8::Isolate::Scope isolate_scope(isolate);

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

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

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

        std::string buf = "function test(arg){return arg;}";

        // Create a string containing the JavaScript source code.
        v8::Local source =
            v8::String::NewFromUtf8(isolate, buf.c_str(), v8::NewStringType::kNormal, static_cast(buf.length())).ToLocalChecked();

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

        v8::Local result = script->Run(context).ToLocalChecked();

        v8::Local global = context->Global();

        v8::Local v = global->Get(context, v8::String::NewFromUtf8Literal(isolate, "test")).ToLocalChecked();

        v8::Local func = v8::Local::Cast(v);

        v8::Local args[1];
        args[0] = v8::String::NewFromUtf8Literal(isolate, "Hello Arg");

        // Run the script to get the result.
        result = func->Call(context, global, 1, args).ToLocalChecked();

        // Convert the result to an UTF8 string and print it.
        v8::String::Utf8Value utf8(isolate, result);
        printf("%s\n", *utf8);
    }
    // Dispose the isolate and tear down V8.
    isolate->Dispose();
    v8::V8::Dispose();
    v8::V8::ShutdownPlatform();
    delete create_params.array_buffer_allocator;
    return 0;
}

运行结果:

C++ Google V8库 源码编译及使用(VS2019)_第22张图片

问题解决

error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2'

debug版本的项目引用了release库,更换debug版的v8_monolith之后如果还出现这个错误,添加预处理器 _ITERATOR_DEBUG_LEVEL=0

C++ Google V8库 源码编译及使用(VS2019)_第23张图片

error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug'

On embedder side pointer compression is DISABLED while on V8 side it's ENABLED

添加预处理器 V8_COMPRESS_POINTERS

C++ Google V8库 源码编译及使用(VS2019)_第24张图片

参考

VS2019最简单编译V8引擎方法_slc1112的博客-CSDN博客

VS2017链接V8引擎库遇到的一些问题_myctime的博客-CSDN博客

你可能感兴趣的:(C++,c++,v8,js)