转自:http://blog.csdn.net/liuzhihan209/article/details/20255611
//
Google V8 引擎使用
V8 引擎是 Google 的一个开源项目,是一个高效的 JavaScript 引擎,它可以作为一个独立的库被嵌入到已有的 C++ 应用之中,为软件的灵活性,扩展性提供可能。使用 V8 的另外一个好处是,你不需要重新学习一本脚本语言,JavaScript 已经广泛的被开发人员,尤其是前端开发人员所使用。
最新版本的下载地址:http://code.google.com/p/v8/wiki/Source
下载方式:git /SVN
两种方式都很简单,在git shell 中输入命令即可。
git 下载地址:http://git-scm.com/download/
下载后安装,打开Git Bash,Linux 命令,切换到要下载的目录,输入 ::git clone git://github.com/v8/v8.git v8 即可,会看到目录下自动创建了V8 文件夹。
SVN类似。
提供已经全部下载好的源码下载地址:
下载好了,就需要编译了,可参考原版的说明,最清晰了。
Building 说明地址:http://code.google.com/p/v8/wiki/BuildingWithGYP
,列举了很多编译的可选项,我们就需要在WIndows下就需要重点参考这里了,地址:http://code.google.com/p/v8/wiki/BuildingWithGYP#Visual_Studio
其实说明的很简单的,需要下载4个工具即可,
1.Python, 注意这里用的2.7的,不要用最新版的3.3的,否者会报错(Python呢 最新版的语法改动导致)
下载地址:http://python.org/downloads/ 选择2.7.6 的即可。
2.cygwin。
可以选择使用SVN,下载地址:svn co http://src.chromium.org/svn/trunk/deps/third_party/cygwin@231940 third_party/cygwin
也可以到CSDN这里下载:http://download.csdn.net/detail/liuzhihan209/6983931
3.ICU.
下载地址:http://pan.baidu.com/s/1kZFWi
4.gyp
下载地址:http://download.csdn.net/detail/liuzhihan209/6983931
(以上很多都可以在Google中SVN 到,但是貌似速度太慢了)
这里只有Python 需要安装,安装之后需要配置系统环境变量,在Path中加入你的Python安装目录即可,我的:D:\Python 2.7.6\;
在V8目录下新建,third_party文件夹,将Cygwin 和Icu 解压后放入,将gyp放入V8\build下即可。
在cmd 中,先切换到V8 目录下,输入 python build\gyp_v8 ,很简单的命令,利用python编译。
一般成功后就会在build下看到all.sln。即表示生成成功了。
打开all.sln 编译生成即可。 个人表示上面没什么特别的难处。
附编译好的lib,Debug和Release。
下载地址:http://download.csdn.net/detail/liuzhihan209/6984147
测试代码 V8_test.
最新代码地址(假如你使用的最新的V8,那么测试代码就需要最新)Google 官方 HelloWorld:https://developers.google.com/v8/get_started
很多人下载的最新的代码,目前百度的很多代码都是旧版的,所以此处会报错。加之对V8类不是很熟悉,不好修改。
附代码:
- <span style="font-size:14px;">// V8_test.cpp : 定义控制台应用程序的入口点。
- //
- //https://developers.google.com/v8/get_started
- #include "stdafx.h"
- #include "v8.h"
- //#pragma comment(lib,"v8_base.ia32.lib")
- //#pragma comment(lib,"v8_nosnapshot.ia32.lib")
- //#pragma comment(lib,"v8_snapshot.lib")
- //v8 need this 使用V8需要链接ws2_32.lib和winmm.lib
- //#pragma comment( lib,"ws2_32.lib" )
- //#pragma comment(lib,"winmm.lib")
- using namespace v8;
- //v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate) {
- // // Create a template for the global object.
- // return v8::Context::New(isolate);
- //}
- int _tmain(int argc, _TCHAR* argv[])
- {
- v8::Isolate* isolate = Isolate::GetCurrent();
- HandleScope handle_scope(isolate);
- Handle<v8::Context> context = Context::New(isolate);
- 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);
- context->Enter();
- // Run the script to get the result.
- Handle<Value> result = script->Run();
- // Dispose the persistent context.
- context->Exit();
- // Convert the result to an ASCII string and print it.
- String::Utf8Value utf8(result);
- printf("%s\n",utf8);
- return 0;
- }
- </span>
编译注意,本人采用的是在编译器链接中加入的附加lib,属性,链接,附加依赖项:ws2_32.lib
v8_base.ia32.lib v8_nosnapshot.ia32.lib v8_snapshot.lib icui18n.lib icuuc.lib winmm.lib,附加包含文件,就一个V8\include,即可。注意附加lib不可少,新版本的多了icui18n.lib,icuuc.lib,貌似不加会报错,可以自己测试下吧。
然后就没有啦,OK,HelloWorld就出来啦。有问题请留言。