1 -- 使用scons编译代码
scons是一个用 Python 语言编写的类似于 make 工具的程序。与 make 工具相比较,scons的配置文件更加简单清晰明。scons依赖于python2.x,所以在开始编译v8之前需要安装好python2.x以及socns。
在这里下载scons:http://nchc.dl.sourceforge.net/project/scons/scons/2.2.0/scons-2.2.0.tar.gz。
下载后按照如下所示方法进行安装。
$root> cd scons-2.2.0 $root> python setup.py install
python2.x以及scons安装成功后,按如下步骤编译V8:
STEP01 通过SVN下载V8源代码。
$svn co svn checkout http://v8.googlecode.com/svn/trunk /objs/v8
STEP02 使用scons编译V8代码。
$ cd /objs/v8 $ scons library=static
如果一切皆顺利,那么在编译输出的最后,将会看到如下的信息:
ranlib libv8preparser.a scons: done building targets. ####################################################### # WARNING: Building V8 with SCons is deprecated and # # will not work much longer. Please switch to using # # the GYP-based build now. Instructions are at # # http://code.google.com/p/v8/wiki/BuildingWithGYP. # #######################################################
该警告信息表明,v8已经不推荐使用scons来编译代码了,取而代之使用gyp编译工具,下面我们就尝试使用gyp来编译v8。
2 -- 使用gyp编译代码
STEP01 通过SVN下载V8源代码。
$svn co svn checkout http://v8.googlecode.com/svn/trunk /objs/v8
STEP02 开始编译。
$ make ia32
编译过程中,请注意有如下几个命令来生成静态库:
AR(target) /objs/v8/out/ia32.release/obj.target/tools/gyp/libpreparser_lib.a AR(target) /objs/v8/out/ia32.release/obj.target/tools/gyp/libv8_base.a AR(target) /objs/v8/out/ia32.release/obj.target/tools/gyp/libv8_nosnapshot.a AR(target) /objs/v8/out/ia32.release/obj.target/tools/gyp/libv8_snapshot.a
在此我们可以找到v8最终生成库的位置。
3 -- 测试v8的例子
v8安装成功后,肯定是迫不及待的想要写个小程序来见识下v8编程的魅力,如果没有合适的例子,可以将如下的helloworld.cpp和Makefile保存下来,直接测试之。
将如下代码段保存为helloworld.cpp:
#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;
}
将如下代码段保存为Makefile:
V8INC := -I/objs/v8/include
V8LIB := /objs/v8/out/ia32.release/obj.target/tools/gyp/libv8_{base,snapshot}.a
all:helloworld
helloworld:helloworld.cpp
g++ -o helloworld helloworld.cpp ${V8INC} ${V8LIB} -lpthread