【问题标题】:V8 compile error for basic example基本示例的 V8 编译错误
【发布时间】:2012-07-02 03:44:51
【问题描述】:

我正在尝试为 V8 编译 hello world 示例,但一直遇到编译时错误。代码如下:

#include <v8/src/v8.h>

using namespace v8;

int main(int argc, char* argv[]) {

  // Create a string holding the JavaScript source code.
  String source = String::New("Hi");

  // Compile it.
  Script script = Script::Compile(source) ;

  // Run it.
  Value result = script->Run();

  // Convert the result to an ASCII string and display it.
  String::AsciiValue ascii(result) ;
  printf("%s\n", *ascii) ;
  return 0;
}

这是编译错误:

error: conversion from ‘v8::Local<v8::String>’ to non-scalar type ‘v8::String’ requested

第 8 行的错误是:String source = String::New("Hi");

我已经尝试用谷歌搜索这个错误,但似乎找不到有意义的修复方法。有什么想法吗?

我都试过了:

svn 结帐http://v8.googlecode.com/svn/trunk/v8

svn 结帐http://v8.googlecode.com/svn/branches/bleeding_edge/v8

两者都得到相同的错误。

【问题讨论】:

  • 哪一行给出了错误?
  • 第 8 行出现错误。我更新了帖子以反映这一点。
  • 您尝试的代码大致解释了发生了什么。您应该使用的真实代码位于本文后面。

标签: c++ v8


【解决方案1】:

根据错误信息,尝试:

Local<String> source = String::New("Hi");

【讨论】:

  • 这修复了错误,但现在我得到一个新错误:未定义引用 `v8::String::New(char const*, int)'| (它没有特别引用任何行)
  • @user396404:现在听起来像是链接器错误。确保你传入了正确的库。
  • 我已经通过了 v8.h。应该有其他图书馆吗?
  • @user396404:这是一个头文件,Javascript 引擎不是一个只有头文件的库。链接时还需要.lib 文件(在 Windows 上)或.a.so 文件(在 Linux 上)。
【解决方案2】:

试试这个代码:

HandleScope handle_scope;
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Handle<String> source = String::New("'Hello' + ', World!'");
Handle<Script> script = Script::Compile(source);
TryCatch trycatch;
Handle<Value> result = script->Run();   
if ( result.IsEmpty() ) {
    Handle<Value> excep = trycatch.Exception();
    String::AsciiValue excep_str(excep);
    printf("%s\n",*excep);
}  else {
    String::AsciiValue ascii(result);
    printf("%s\n", *ascii);
}
context.Dispose();
return 0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多