4

我正在尝试使用 Google 的 V8 JavaScript Engine 编译文件。我安装scons并编译了 V8 引擎。但是,这就是问题所在,我按照他们所说的那样留在 V8 目录中并创建一个以hello_world.cpp代码命名的文件:

#include <v8.h>

using namespace v8;

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

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

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

      // 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.
      Handle<String> source = String::New("'Hello' + ', World!'");

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

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

      // Dispose the persistent context.
      context.Dispose();

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

然后我使用gcc hello_world.cpp -o libv8.a. 但是,当我编译它时,我得到了一些错误:

hello_world.cpp:1:16: error: v8.h: No such file or directory
hello_world.cpp:3: error: ‘v8’ is not a namespace-name
hello_world.cpp:3: error: expected namespace-name before ‘;’ token
hello_world.cpp: In function ‘int main(int, char**)’:
hello_world.cpp:8: error: ‘HandleScope’ was not declared in this scope
hello_world.cpp:8: error: expected `;' before ‘handle_scope’
hello_world.cpp:11: error: ‘Persistent’ was not declared in this scope
hello_world.cpp:11: error: ‘Context’ was not declared in this scope
hello_world.cpp:11: error: ‘context’ was not declared in this scope
hello_world.cpp:11: error: ‘Context’ is not a class or namespace
hello_world.cpp:15: error: ‘Context’ is not a class or namespace
hello_world.cpp:15: error: expected `;' before ‘context_scope’
hello_world.cpp:18: error: ‘Handle’ was not declared in this scope
hello_world.cpp:18: error: ‘String’ was not declared in this scope
hello_world.cpp:18: error: ‘source’ was not declared in this scope
hello_world.cpp:18: error: ‘String’ is not a class or namespace
hello_world.cpp:21: error: ‘Script’ was not declared in this scope
hello_world.cpp:21: error: ‘script’ was not declared in this scope
hello_world.cpp:21: error: ‘Script’ is not a class or namespace
hello_world.cpp:24: error: ‘Value’ was not declared in this scope
hello_world.cpp:24: error: ‘result’ was not declared in this scope
hello_world.cpp:30: error: ‘String’ is not a class or namespace
hello_world.cpp:30: error: expected `;' before ‘ascii’
hello_world.cpp:31: error: ‘ascii’ was not declared in this scope
hello_world.cpp:31: error: ‘printf’ was not declared in this scope

我不明白为什么它说 V8.h 没有声明。我已经构建了它并且我在它的目录中,我猜如果我摆脱它,所有其他错误都会消失。有什么建议么?

4

1 回答 1

3

我只是相信你真的在顶级源目录中(因为我没有编译 v8,所以我只相信 libvp8.a 是在那个顶级目录中创建的):

% g++ -Iinclude hello_world.cpp -o hello_world libv8.a
  1. 它说“v8.h”未声明,因为该文件位于“include”目录中,预处理器无法凭空找到它。

  2. 此外:您正在使用 C 编译器而不是 C++ 编译器编译 .cpp 文件。

  3. 您使用的“-o”标志错误,因为它定义了链接二进制文件的名称,因此需要一个名称,您不希望输出二进制文件命名为“libvp8.a”

于 2010-06-28T17:41:55.033 回答