gpt4 book ai didi

macos - V8编译问题

转载 作者:行者123 更新时间:2023-12-01 02:55:31 26 4
gpt4 key购买 nike

我正在尝试使用谷歌的 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。我已经构建了它并且我在它的目录中,我猜如果我摆脱了它,所有其他错误都会消失。有什么建议?

最佳答案

我只是相信你确实在顶级源目录中(并且由于我没有编译 v8,我只相信 libvp8.a 是在该顶级目录中创建的):

% g++ -Iinclude hello_world.cpp -o hello_world libv8.a
  • 它说未声明“v8.h”,因为该文件位于“include”目录中,并且预处理器无法凭空找到它。
  • 此外:您正在使用 C 编译器而不是 C++ 编译器编译 .cpp 文件。
  • 您使用的“-o”标志是错误的,因为它定义了链接二进制文件的名称,因此需要一个名称,您不希望输出二进制文件被命名为“libvp8.a”
  • 关于macos - V8编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3136260/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com