Google V8 编译方法(visual c++ 2008 express)(学习+原创)

参考资料:

 

(不同 VS 版本的可以看这里,有很详细介绍的,不过是鸟文··)http://code.google.com/p/v8/wiki/BuildingOnWindows

http://code.google.com/p/v8/

http://www.cnblogs.com/sweetwxh/archive/2011/09/11/2173596.html

 

 

 

一、准备工作

 

1         TortoiseSVN

 

2          V8   Engine源代码,从Google的SVN当中Check Out出来,最新版本地址。

 

3          Python 2.6.x  (必须是 2.6版本的)

 

4          Scons,与Make工具类似的编译工具

 

 

二、 下载安装:

 1.  安装  tortoiseSVN ,

 2 .   根据V8 的地址 (http://v8.googlecode.com/svn/trunk/)下载下来:

 3.  安装Python2.6.x(我安装的是 2.6.6 ), 设置环境变量 

 4.  安装Scons,Scons也需要设置环境变量,它的目录是Python安装目录下的Scripts。

 

注意: python 安装之后,才安装 scons ,scons是安装在python的目录下的。

 

三、编译(我用的是 , visual  c++ 2008 express)

1. 打开 CMD

2.导航到下载的v8源代码根目录,就是有一个SConstruct文件(Scons就是根据它来编译程序的)的目录。

3. 使用 scons在该目录下 编译, 编译参数不同版本会不一样,

可以看:http://code.google.com/p/v8/wiki/BuildingOnWindows

这里只介绍 visual c++ 2008 express 的

 

先看几个参数:(其他的参数可以去官网看)

(1)       env      :   env="INCLUDE:%INCLUDE%,LIB:%LIB%"   (具体路径看下面)

(2)       mode    :   release /debug

(3)       msvcrt   :   shared/static (动态库/静态库)

 

根据自己的需求编译,编译完成后,会在根目录生产v8.lib, v8.dll ,  v8_g.lib v8_g.dll, 在其他程序中使用V8需要用到这两个库文件以及v8的头文件。

建议编译成动态库,静态库的话很大,而且HELLOWORD 编译链接的时候都要很久。

 

例子:

(1)(编译为 release 版本, 动态库)

scons env="INCLUDE:C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include;C:\Program Files\Microsoft Visual Studio 9.0\VC\include,LIB:C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib;C:\Program Files\Microsoft Visual Studio 9.0\VC\lib"libary=shared mode=release msvcrt=shared –j4

 

 

(2)编译为 DEBUG 版本, 动态链接库

scons env="INCLUDE:C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include;C:\Program Files\Microsoft Visual Studio 9.0\VC\include,LIB:C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib;C:\Program Files\Microsoft Visual Studio 9.0\VC\lib"libary=shared mode=debug msvcrt=shared verbose=on

 

 

或者

(1)       打开VS里面的选项 : tools\visual studio 2008 command promopt

或者

进入Windows平台,进入命令后先运行,以初始化vc编译环境。

cmd /k ""C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86

 

(2)       导航到下载的v8源代码根目录

(3)       不用写 env 的参数

(4)       根据需要选择编译参数:

(5)       例如:

scons mode=debug verbose=on library=shared msvcrt=static

 

 

 

建个自己的 Hello World!

 

(1)     从官网下代码:

代码如下:

 

#include <v8.h>

using namespace v8;

int main(int argc, char* argv[]) {

  // Create a stack-allocated handle scope.
  HandleScope handle_scope;

  // Create a new context.
  Persistent<Context> context = Context::New();
 
  // Enter the created 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::New("'Hello' + ', World!'");

  // Compile the source code.
  Handle<Script> script = Script::Compile(source);
 
  // Run the script to get the result.
  Handle<Value> result = script->Run();
 
  // Dispose the persistent context.
  context.Dispose();

  // Convert the result to an ASCII string and print it.
  String::AsciiValue ascii(result);
  printf("%s\n", *ascii);
  return 0;
}

 

(2)把 头文件 和.lib 文件包含进去

 

如果编译的时候出现链接错误 ,可以把这两个库也加进去,就可以啦

 "C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib\Winmm.lib"

 "C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib\WSock32.lib"

 

(3)运行的结果就是: 控制台输出 :hello world!


posted @ 2012-02-27 13:43  吴豆豆  阅读(1398)  评论(0编辑  收藏  举报