google_v8学习笔记:NO2 第一个代码Hello_word

在学习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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值