v8編譯和測試程式

來源:互聯網
上載者:User
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.ascons: 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.aAR(target) /objs/v8/out/ia32.release/obj.target/tools/gyp/libv8_base.aAR(target) /objs/v8/out/ia32.release/obj.target/tools/gyp/libv8_nosnapshot.aAR(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/includeV8LIB  := /objs/v8/out/ia32.release/obj.target/tools/gyp/libv8_{base,snapshot}.aall:helloworldhelloworld:helloworld.cpp    g++ -o helloworld helloworld.cpp ${V8INC} ${V8LIB} -lpthread 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.