2012-07-02 54 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;