2013-06-05 17 views
0

我嘗試使用:許多錯誤回報 - 的JavaScript

git clone git://github.com/v8/v8.git v8 && cd v8 or svn checkout http://v8.googlecode.com/svn/trunk/ v8 

使用庫:

make dependencies 
sudo apt-get install apt-file; 
sudo apt-get install libc6-dev-i368 lib32stdc++6; 

當我嘗試編譯一個簡單的文件爲:

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

    // Create a string containing the JavaScript source code. 
    String source = String::New("'Hello' + ', World'"); 

    // Compile the source code. 
    Script script = Script::Compile(source); 

    // Run the script to get the result. 
    Value result = script->Run(); 

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

該命令用途:

g++ test.cpp -Ideps/v8/include -Ldeps/v8/ -lv8 -lpthread 

我得到了如下錯誤輸出:

v8/src/ 

test.cpp: Na função ‘int main(int, char**)’: 
test.cpp:4:3: error: ‘String’ was not declared in this scope 
test.cpp:4:10: error: expected ‘;’ before ‘source’ 
test.cpp:7:3: error: ‘Script’ was not declared in this scope 
test.cpp:7:10: error: expected ‘;’ before ‘script’ 
test.cpp:10:3: error: ‘Value’ was not declared in this scope 
test.cpp:10:9: error: expected ‘;’ before ‘result’ 
test.cpp:13:3: error: ‘String’ is not a class or namespace 
test.cpp:13:22: error: expected ‘;’ before ‘ascii’ 
test.cpp:14:19: error: ‘ascii’ was not declared in this scope 
test.cpp:14:24: error: ‘printf’ was not declared in this scope 

問題是什麼?有人能指引我朝着正確的方向嗎?

回答

0

您嘗試編譯的C++代碼是V8「入門指南」中的部分示例(「僞代碼」)。它無法正常工作。下面同樣的文檔說明了你需要的運行方式:

  1. 包含v8.h頭文件並將項目鏈接到靜態(或共享)V8庫。
  2. 導入v8命名空間(using namespace v8);
  3. 獲取參考/指向默認隔離的指針(Isolate::GetCurrent())。
  4. 爲本地句柄創建一個HandleScope。
  5. 創建並輸入V8執行上下文。
  6. 只有這樣你才能使用類似上面的代碼。

有關詳細信息,請參閱V8 Getting Started Guide

1

根據@Sim的回答,這裏是工作版本。

#include "v8.h" 
using namespace v8; 


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

    // Get the default Isolate created at startup. 
    Isolate* isolate = Isolate::GetCurrent(); 

    // Create a stack-allocated handle scope. 
    HandleScope handle_scope(isolate); 

    // Create a new context. 
    Handle<Context> context = Context::New(isolate); 

    // Here's how you could create a Persistent handle to the context, if needed. 
    Persistent<Context> persistent_context(isolate, context); 

    // Enter the created 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::New("'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 ASCII string and print it. 
    String::AsciiValue ascii(result); 
    printf("%s\n", *ascii); 
    return 0; 
} 

要編譯並運行它,假設你有../lib

g++ -L../lib -I../include hello.cc -o a.out -lv8_base.x64 -lv8_snapshot -lpthread && ./a.out 
../include V8頭文件和庫V8