在学习google_v8的时候也看了很多代码,但是发现总是运行不了,再到官网中看,示例代码已经变了,现在附上一个官方的示例代码:
#include <v8.h>
using namespace v8;
int main(int argc, char* argv[]) {
// 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);
return 0;
}
编译运行时候需要增加几个库libv8_base.a,libv8_libbase.a,libv8_snapshot.a,以及libicuuc.a,libicui18n.a,libicudata.a
g++ -I../../include helloword.cc -o hello_world -Wl,--start-group ../../out/x64.release/obj.target/tools/gyp/libv8_{base,libbase,snapshot}.a ../../out/native/obj.target/third_party/icu/libicu{uc,i18n,data}.a -Wl,--end-group -lrt -pthread
如果 少了-pthread参数的话可能报错,错误如下
/base/platform/mutex.o: undefined reference to symbol 'pthread_mutexattr_settype@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
运行后在当前目录下生成hello_word文件
运行:
./hello_word
相对于之前的实例文件多了以下代码,定于一个新的运行内存区
Isolate* isolate = Isolate::New();
Isolate::Scope isolate_scope(isolate);
对于在文件问出现的string,value等变量,我们可以再object.cc/h中找到,上面就是一个简单的示例文件,后面我们将慢慢学习下google_v8内核代码部分
frighting-----sixi