v8 developer官方主页:https://developers.google.com/v8/
v8 代码下载和编译官方主页:https://developers.google.com/v8/build
其实这里面对代码的下载和编译的相关内容已经比较详细了,但是这也意味着并不完整,简而言之我按着他的步骤编译不出来,如下是我的具体步骤(红色部分为非官方提供的步骤):
1.下载源代码:svn checkout http://v8.googlecode.com/svn/trunk/ v8
2.安装depot_tools(用来管理代码和审查以及得到gclient目录):
2.1 svn co http://src.chromium.org/svn/trunk/tools/depot_tools2
2.2 设置环境变量:$ export PATH=`pwd`/depot_tools:"$PATH"
3.make dependencies获取各种make需要的依赖
4.make ia32.release 或者 make ia32.release library=shared
5.编译例子:
例子基本是v8官网已经很详细了:https://developers.google.com/v8/get_started
首先创建一个.cc源文件:
#include "include/v8.h" #include "include/libplatform/libplatform.h" using namespace v8; int main(int argc, char* argv[]) { // Initialize V8. V8::InitializeICU(); Platform* platform = platform::CreateDefaultPlatform(); V8::InitializePlatform(platform); V8::Initialize(); // Create a new Isolate and make it the current one. Isolate* isolate = Isolate::New(); { Isolate::Scope isolate_scope(isolate); // Create a stack-allocated handle scope. HandleScope handle_scope(isolate); // Create a new context. Local<Context> context = Context::New(isolate); // Enter the context for compiling and running the hello world script. Context::Scope context_scope(context); // Create a string containing the JavaScript source code. Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'"); // Compile the source code. Local<Script> script = Script::Compile(source); // Run the script to get the result. Local<Value> result = script->Run(); // Convert the result to an UTF8 string and print it. String::Utf8Value utf8(result); printf("%s\n", *utf8); } // Dispose the isolate and tear down V8. isolate->Dispose(); V8::Dispose(); V8::ShutdownPlatform(); delete platform; return 0; }编译的命令:
g++ -I. hello_world.cc -o hello_world -Wl,--start-group out/x64.release/obj.target/{tools/gyp/libv8_{base,libbase,snapshot,libplatform},third_party/icu/libicu{uc,i18n,data}}.a -Wl,--end-group -lrt -pthread
执行:
./hello_world