2011
Oct
27

這篇是 2012/08 前舊的安裝方式,最新的安裝方式可以參考 此處

安裝前說明

在試玩 node.js前,先來試用一個 V8 Engine 的功能吧,不過要玩V8前,總得先把 V8 Engine裝起來 XD。首先你要先裝好 svn,或是用windows的tortoise svn 抓下來,再 copy 到 Linux上 。

安裝 SCons

Google V8 Engine 是用 SCons 取代 make 來達成編譯的動作。

安裝 python

更新 gcc版本

我原本使用 gcc-3.4.6 版,結果編譯 v8 engine 時會有一些問題,後來更新到 gcc-4.2.0 後,就可以正常編譯了。

如何更新 gcc呢,這又是一個大工程, gcc-4.2.0 版會用到 GMP 與 MPFR ,所以我們要先安裝這兩個 package,請先下載來安裝吧。


  • 載點 ftp://ftp.gmplib.org/pub/gmp-5.0.2/gmp-5.0.2.tar.gz
  • ./configure --prefix=/usr/local/gmp-5.0.2
  • make
  • sudo make install
  • -------------
  • 載點 http://www.mpfr.org/mpfr-current/mpfr-3.1.0.tar.gz
  • ./configure --prefix=/usr/local/mpfr-3.1.0
  • make
  • sudo make install

最後終於可以安裝 GCC 囉

  • 載點 http://gcc.skazkaforyou.com/releases/gcc-4.2.0/gcc-4.2.0.tar.bz2
  • ./configure --with-gmp=/usr/local/gmp-5.0.2 --with-mpfr=/usr/local/mpfr-3.1.0 --prefix=/usr/local/gcc4 --enable-shared --enable-languages=c,c++ --enable-threads=posix --disable-checking --with-system-zlib
  • make (這裡會跑很久,大約一小時)
  • sudo make insall
  • 裝完後,再把 gcc cp 到系統執行的位置
    • sudo cp /usr/local/gcc4/bin/gcc /usr/bin
    • sudo cp /usr/local/gcc4/bin/g++ /usr/bin
    • sudo cp /usr/local/gcc4/libstdc++* /usr/lib , 會用到的library 也要記得搬過去

輸入指令 gcc -v : 檢查版本是否正確

檢查 Pre-requisites

  • svn --version : 版本要高於 Subversion 1.4
  • python -V : 版本要高於 Python 2.4
  • scons --version : 版本要高於 SCons 1.0.0

安裝 v8 engine

  • svn checkout http://v8.googlecode.com/svn/trunk/
  • sudo scons mode=release library=shared snapshot=on
    • mode=debug 就是指debug模式囉
    • mode=release 正式
  • sudo scons sample=shell
  • sudo scons d8

順利跑完的話,會産生一個 d8,libv8.so 檔案,把他copy到可執行的 dir 吧

  • sudo cp d8 /usr/local/bin/
  • sudo cp libv8.so /usr/local/lib/
  • sudo cp include/* /usr/local/include/
  • sudo ldconfig (重新建立 Link Editor )
  • 修改 /etc/ld.so.conf ,加入 /usr/local/lib
  • 執行 sudo ldconfig

Error處理

錯誤訊息 : scons: *** [obj/release/accessors.o] Error 1

Example
  1. [puritys]trunk$ sudo scons sample=shell
  2. .
  3. .
  4. src/heap.h: In member function 'v8::internal::byte** v8::internal::Heap::store_buffer_top_address()':
  5. src/heap.h:1103: warning: dereferencing type-punned pointer will break strict-aliasing rules
  6. scons: *** [obj/release/accessors.o] Error 1
  7. scons: building terminated because of errors.

把 trunk/SConstruct 的 '-Werror' 刪掉 or 註解

錯誤訊息 : error invalid Python installation: unable to open /usr/lib/python2.4/config/Makefile (No such file or directory)

安裝 python2.4-devel-2.4-1pydotorg.i386.rpm

hidden symbol `v8::String::New(char const*, int)' in /usr/local/lib/libv8.a(api.o) is referenced by DSO

這行訊息代表你使用了 v8 的靜態 library , libv8.a ,請改用 shared library , libv8.so 即可。

試用 v8 engine :「Hello World!」

Example
  1. #include <v8.h>
  2. using namespace v8;
  3.  
  4. int main(int argc, char* argv[]) {
  5.  
  6. HandleScope handle_scope;
  7. Persistent<Context> context = Context::New();
  8.  
  9. Context::Scope context_scope(context);
  10. //定義一個字串
  11. Handle<String> source = String::New("'Hello' + ', World!'");
  12.  
  13. Handle<Script> script = Script::Compile(source);
  14.  
  15. Handle<Value> result = script->Run();
  16.  
  17. context.Dispose();
  18. String::AsciiValue ascii(result);
  19. printf("%s\n", *ascii);
  20. return 0;
  21. }
  22.  
  • g++ example1.cc -lv8 -pthread -o test
  • ./test
Example
  1. [puritys]V8$ g++ example1.cc -lv8 -pthread -o test
  2. [puritys]V8$ ./test
  3. Hello, World!

『註1』 2012年07月27日,v8 編譯已可以支援 GCC 3.x ,當然這是官方說明,我自已沒有試過,有需要的人,可以試試看在 GCC 3.x 編譯 v8


回應 (Leave a comment)