2012-07-02 51 views
0

我想编译V8的hello世界示例,并且我一直运行到编译时错误。下面是代码:V8编译错误的基本示例

#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,其中它说:字符串源=字符串::新( “你好”);

我试过google'ing这个错误没有意义,并且似乎无法找到修复它是有道理的。有任何想法吗?

我曾经尝试都:

svn签http://v8.googlecode.com/svn/trunk/ V8

svn签http://v8.googlecode.com/svn/branches/bleeding_edge/ V8

,并得到了同样的错误两种。

+0

哪条线给你的错误? – AnT

+0

错误在第8行。我更新了帖子以反映这一点。 – user396404

+0

您尝试的代码通常会解释发生了什么。您应该使用的真实代码位于文章后面。 –

回答

1

基于错误信息,请尝试:

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

修正了错误,但现在我得到一个新的错误:未定义的引用'v8 :: String :: New(char const *,int)'| (它没有特别引用任何行) – user396404

+1

@ user396404:现在听起来像是一个链接器错误。确保你通过了正确的库。 –

+0

我已经在v8.h中传递。应该有其他的图书馆吗? – user396404

0

试试这个代码:

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;