V8编译问题

发布于 2024-09-08 02:02:13 字数 2915 浏览 2 评论 0原文

我正在尝试使用 Google 的 V8 JavaScript 引擎编译一个文件。我安装了 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 未声明。我已经构建了它并且位于它的目录中,我猜如果我摆脱它,所有其他错误都会消失。有什么建议吗?

I'm trying to compile a file with the V8 the JavaScript Engine by Google. I installed scons and have compiled the V8 engine. But, here is where the problem lies, I stay in the V8 directory as they say and make a file named hello_world.cpp with the code:

#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;
    }

Then I compile using gcc hello_world.cpp -o libv8.a. But, when I compile it I get a skew of errors:

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

I don't get why it say V8.h is not declared. I already built it and I'm in its directory and I'm guessing if I get rid of that all the other errors will go away. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽梦忆笙歌 2024-09-15 02:02:14

我只是相信你确实在顶级源目录中(并且由于我没有编译 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”

i just believe you in that you are really inside the toplevel source directory (and since i do not have compiled v8 i only believe that libvp8.a is created in that toplevel directory):

% g++ -Iinclude hello_world.cpp -o hello_world libv8.a
  1. it says "v8.h" is not declared because that file is inside the "include" directory and the preprocessor is not able to find it out of thin air.

  2. furthermore: you are compiling a .cpp file with the C compiler instead of the C++ compiler.

  3. you are using the '-o' flag wrong because it defines the name of the linked binary and thus needs a name, you do not want the output binary be named "libvp8.a"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文