必须要有代理
准备
配置临时环境变量
管理员身份运行控制台(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
代理端口查看设置里面的
因为是临时环境变量,每次打开控制台都需要重新设置。
下载 depot tools
https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html#_setting_up
?点击 budle 下载后解压
?将该目录加入到环境变量path中
?执行 gclient
?出现这个提示表示下载完成。?
安装windows sdk10
直接在vs中安装就行了
下载源码
fetch v8
如果中途失败,继续执行 gclient sync
?出现这个提示表示下载完成。
关于路径问题
如果VS的安装目录和windows sdk的安装目录都是默认在C盘,可以跳过。
这里会出现找不到 VS 的错误。
编辑文件 build/vs_toolchain.py
177行左右,直接将path改为vs的路径。
204行左右,直接返回vs路径
编译?
使用 clang libc++ 编译动态库
根据需要编译的平台选择相应的控制台,64位的选择x64,32位的选择x86
因为是新开控制台,需要重新设置?
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
可以生成 vs 解决方案
gn gen --ide=vs out.cl\x64.debug
可以直接打开vs编译,也可以继续命令行编译
?按照网上说法直接编译 gn_all
或者命令行编译
ninja -C out.cl/x64.debug v8
?
?打开解决方案
?查看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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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<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;
}
?运行结果:
如果用vs创建项目使用v8库,会报链接错误:
通过查看v8_libplatform.dll.lib的导出符号
发现:?
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
顺便生成 vs 解决方案
gn gen --ide=vs out\x64.release
命令行编译
ninja -C out/x64.release v8_monolith
测试执行js文件:
创建一个控制台应用,如下设置
?
#include "libplatform/libplatform.h"
#include "v8-context.h"
#include "v8-initialization.h"
#include "v8-isolate.h"
#include "v8-local-handle.h"
#include "v8-primitive.h"
#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<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);
char* s = NULL;
_get_pgmptr(&s);
std::string path(s);
size_t 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<char> 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);
// Create a string containing the JavaScript source code.
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, buf.get(), v8::NewStringType::kNormal, static_cast<int>(len)).ToLocalChecked();
// 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);
}
}
// Dispose the isolate and tear down V8.
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete create_params.array_buffer_allocator;
return 0;
}
?t.js
function hello() {
return "hello!";
}
hello();
运行结果:
参考
VS2019最简单编译V8引擎方法_slc1112的博客-CSDN博客
VS2017链接V8引擎库遇到的一些问题_myctime的博客-CSDN博客
|