【问题标题】:Many erros when i try compile c++ in V8 - JavaScript当我尝试在 V8 中编译 c++ 时出现许多错误 - JavaScript
【发布时间】:2013-06-05 20:54:38
【问题描述】:

我尝试使用:

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

有什么问题?有人能指出我正确的方向吗?

【问题讨论】:

    标签: javascript v8


    【解决方案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;
    }
    

    要编译和运行它,假设您在 ../include 中有 v8 头文件,在 ../lib 中有 v8 库

    g++ -L../lib -I../include hello.cc -o a.out -lv8_base.x64 -lv8_snapshot -lpthread && ./a.out
    

    【讨论】:

      【解决方案2】:

      您尝试编译的 C++ 代码是摘自 V8“入门”指南的部分示例(“伪代码”)。它不能按原样工作。运行此程序所需的内容在下面的同一文档中进行了说明:

      1. 包含 v8.h 标头并将您的项目链接到静态(或共享)V8 库。
      2. 导入 v8 命名空间 (using namespace v8);
      3. 获取默认 Isolate (Isolate::GetCurrent()) 的引用/指针。
      4. 为本地句柄创建 HandleScope。
      5. 创建并进入 V8 执行上下文。
      6. 只有这样你才能使用上面的代码。

      详情请参阅V8 Getting Started Guide

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-22
        相关资源
        最近更新 更多