gpt4 book ai didi

c++ - mac 上的 node-gyp 构建错误 "calling a protected constructor of class ' v8::HandleScope'"

转载 作者:太空宇宙 更新时间:2023-11-04 13:02:51 25 4
gpt4 key购买 nike

我使用 node-gyp 为 node 构建我的 c++ 库,并在网上复制了一些示例。

回调.cc

#include <node.h>
#include <v8.h>
using namespace v8;
Handle<Value> Method(const v8::internal::Arguments& args){
HandleScope scope;
return scope.Close(String::New("hello,world"));
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("hello"),FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)

绑定(bind).gyp

{
"targets": [
{
"target_name": "hello",
"sources": [ "callback.cc" ]
}
]
}

运行 node-gyp configure 确定。运行 node-gyp build 失败,错误:

gyp info it worked if it ends with ok
gyp info using node-gyp@3.6.0
gyp info using node@7.9.0 | darwin | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
CXX(target) Release/obj.target/hello/callback.o
../callback.cc:5:15: error: calling a protected constructor of class 'v8::HandleScope'
HandleScope scope;
^
/Users/zhouweiming/.node-gyp/7.9.0/include/node/v8.h:908:13: note: declared protected here
V8_INLINE HandleScope() {}
^
../callback.cc:6:16: error: no member named 'Close' in 'v8::HandleScope'
return scope.Close(String::New("hello,world"));
~~~~~ ^
../callback.cc:6:30: error: no member named 'New' in 'v8::String'
return scope.Close(String::New("hello,world"));
~~~~~~~~^
../callback.cc:9:65: error: cannot initialize a parameter of type 'v8::Isolate *' with an lvalue of type 'Handle<v8::Value> (const v8::internal::Arguments &)'
(aka 'v8::Local<v8::Value> (const v8::internal::Arguments &)')
exports->Set(String::NewSymbol("hello"),FunctionTemplate::New(Method)->GetFunction());
^~~~~~
/Users/zhouweiming/.node-gyp/7.9.0/include/node/v8.h:5079:16: note: passing argument to parameter 'isolate' here
Isolate* isolate, FunctionCallback callback = 0,
^
../callback.cc:9:24: error: no member named 'NewSymbol' in 'v8::String'
exports->Set(String::NewSymbol("hello"),FunctionTemplate::New(Method)->GetFunction());
~~~~~~~~^
5 errors generated.
make: *** [Release/obj.target/hello/callback.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/.node-gyp_npminstall/node_modules/.node- gyp@3.6.0/lib/build.js:285:23)
gyp ERR! stack at emitTwo (events.js:106:13)
gyp ERR! stack at ChildProcess.emit (events.js:194:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Darwin 16.5.0
gyp ERR! command "/usr/local/Cellar/node/6.9.1/bin/node" "/usr/local/bin/node-gyp" "build"
gyp ERR! cwd /Users/zhouweiming/data/project/electron-projects/dll
gyp ERR! node -v v7.9.0
gyp ERR! node-gyp -v v3.6.0
gyp ERR! not ok

我不是 C++ 程序员,请帮助我。

Node :7.9.0

Node 石膏:3.6.0

代码:8.1

苹果电脑:10.12.4

海湾合作委员会:配置为:--prefix=/Applications/Xcode.app/Contents/Developer/usr--with-gxx-include-dir=/usr/include/c++/4.2.1苹果 LLVM 版本 8.0.0 (clang-800.0.42.1)目标:x86_64-apple-darwin16.5.0线程模型:posix安装目录:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

最佳答案

我已经解决了。因为demo中的c++代码已经过时了。

新代码如下:

#include <node/v8.h>
#include <node/node.h>

using namespace node;
using namespace v8;

void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
v8::HandleScope scope(isolate);
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "Hello world"));
}

void init(v8::Local<v8::Object> target) {
NODE_SET_METHOD(target, "hello", Method);
}

NODE_MODULE(binding, init);

网上过时的例子太多了,我会保留这个问题

关于c++ - mac 上的 node-gyp 构建错误 "calling a protected constructor of class ' v8::HandleScope'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43485250/

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