V8是一个由丹麦Google开发的开源JavaScript引擎, 环境 win7 64位+visual studio
参考文档: 官网帮助http://code.google.com/p/v8/wiki/BuildingWithGYP
Visual Studio
首先我们需要安装一个tortoisesvn工具。官网http://tortoisesvn.net/downloads.html
下载对应的64位版本打开运行 默认一个控件不安装 选择所有控件都安装 。
在 硬盘的D盘下面新建个V8文件夹 用来存在V8引擎的源代码 右击文件名
点击SVN checkout
在URL 中填入
http://v8.googlecode.com/svn/trunk
点击ok 自动开始下载文件
将SVN的安装目录添加到环境变量 系统变量 PATH里面。
启动CMD
根据官方帮助 需要下载3个依赖集。
Python cygwin icu gyp
1、python 安装
在命令行中输入
svn co http://src.chromium.org/svn/trunk/tools/third_party/python_26@89111 third_party/python_26
将C:\Users\xiaofan\third_party\python_26 添加到系统环境变量中
2、cygwin 安装
svn co http://src.chromium.org/svn/trunk/deps/third_party/cygwin@231940 third_party/cygwin
3、icu 安装
svn co https://src.chromium.org/chrome/trunk/deps/third_party/icu46 third_party/icu
4、Gyp安装
svn co http://gyp.googlecode.com/svn/trunk build/gyp
进入 GYP目录 在CMD python setup.py install 安装
5、生成all.sln文件
进入V8根目录下
python build\gyp_v8 如果添加了环境变量重启CMD 运行
没有则 third_party/python_26/python.exe build\gyp_v8 -Dtarget_arch=x64
运行完成之后在V8 build 下面会生成解决方案all.sln 直接使用visual studio 打开 编译生成我们需要的库
特别注意,此时有三个编译选项:
1)、指定vs版本:-G msvs_version=2010,这里指定后续使用vs2010打开sln。
2)、指定静态或动态库:-Dcomponent=shared_library,这里指定sln编译生成v8动态库,如果不加这个选项默认是v8静态库
3)、指定sln库的版本:-Dtarget_arch=64,这里指定生成64位的sln,当然需要64位的vs打开,如果不加这个选项默认是32位
由于我的环境是64+VS2012 期望生成动态
python build\gyp_v8 -G msvs_version=2012 -Dcomponent=shared_library
我得到
只需要在你的程序中 #include”v8.h”
#pragma comment(lib,”v8.lib”)
所有的头文件都在V8的include文件里面 copy到工程目录
需要的库都在gyp 的Debug 或者RElese 里面
将这3个DLL 复制到跟程序一个目录 实现 就行。
例子:
#include "v8.h"
#include<Windows.h>
#include<stdio.h>
#pragma comment(lib,"Winmm.lib")
#pragma comment (lib,"v8.lib")
using namespace v8;
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[]) {
// Get the default Isolate created at startup.
Isolate* isolate = Isolate::GetCurrent();
// Create a stack-allocated handle scope.
HandleScope handle_scope(isolate);
// Create a new context.
Handle<Context> context = Context::New(isolate);
// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Handle<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
// Compile the source code.
Handle<Script> script = Script::Compile(source);
// Run the script to get the result.
Handle<Value> result = script->Run();
// Convert the result to an UTF8 string and print it.
String::Utf8Value utf8(result);
printf("%s\n", *utf8);
system("pause");
return 0;
}