gpt4 book ai didi

c++ - 为什么 Hello World for V8 在 Ubuntu 上会导致段错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:44:24 26 4
gpt4 key购买 nike

我编译了V8在 Ubuntu 14.04 上,我现在正在尝试获取样本 hello_world.cc工作,但是,当我执行它时,我得到一个 Segmentation fault (core dumped)

这是我的 hello_world.cc 来源:

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

return 0;
}

按照说明,这是我用来将 hello_world.cc 构建为可执行文件的命令:

g++ -Iinclude -g hello_world.cc -o hello_world -Wl,--start-group out/x64.debug/obj.target/{tools/gyp/libv8_{base,snapshot},third_party/icu/libicu{uc,i18n,data}}.a -Wl,--end-group -lrt -lpthread

请注意,除了说明之外,我还必须添加 -lpthread 标志以使其编译,并添加 -g 以包含调试符号。

这是程序的输出:

$ ./hello_world
Segmentation fault (core dumped)

如果我运行 gdb hello_world core 我会得到:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./hello_world'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00000000004148bb in v8::HandleScope::Initialize (this=0x7fff9b86a110, isolate=0x0) at ../src/api.cc:572
572 prev_next_ = current->next;

第 572 行来自 src/api.cc is here

最佳答案

添加对 Isolate* isolate = Isolate::GetCurrent(); 返回 NULL 的检查:

Isolate* isolate = Isolate::GetCurrent();
if(!isolate) {
isolate = v8::Isolate::New();
isolate->Enter();
}

关于c++ - 为什么 Hello World for V8 在 Ubuntu 上会导致段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23822761/

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