[英]Make failed while trying to build a nodejs native addon
I am trying to create a native nodejs addon (node v16.19) using node-gyp.我正在尝试使用 node-gyp 创建一个本地 nodejs 插件(节点 v16.19)。 The C++ code looks fine, and gives no warnings in my IDE. 'node-gyp configure' runs ok, but when I run 'node-gyp build' I get the stack below. C++ 代码看起来不错,在我的 IDE 中没有给出任何警告。“node-gyp configure”运行正常,但是当我运行“node-gyp build”时,我得到下面的堆栈。 It seems it has something to do with the NODE_SET_METHOD or NODE_MODULE calls I took from this page: https://nodejs.org/docs/latest-v16.x/api/addons.html#factory-of-wrapped-objects它似乎与我从该页面获取的 NODE_SET_METHOD 或 NODE_MODULE 调用有关: https://nodejs.org/docs/latest-v16.x/api/addons.html#factory-of-wrapped-objects
CXX(target) Release/obj.target/addon/source/index.o
In file included from ../source/index.cc:1:
/home/matthias/.cache/node-gyp/16.19.0/include/node/node.h:887:7: warning: cast between incompatible function types from ‘void (*)(v8::Local<v8::Object>)’ to ‘node::addon_register_func’ {aka ‘void (*)(v8::Local<v8::Object>, v8::Local<v8::Value>, void*)’} [-Wcast-function-type]
887 | (node::addon_register_func) (regfunc), \
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/matthias/.cache/node-gyp/16.19.0/include/node/node.h:921:3: note: in expansion of macro ‘NODE_MODULE_X’
921 | NODE_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage)
| ^~~~~~~~~~~~~
../source/index.cc:60:3: note: in expansion of macro ‘NODE_MODULE’
60 | NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
| ^~~~~~~~~~~
In file included from /home/matthias/.cache/node-gyp/16.19.0/include/node/node.h:73:
/home/matthias/.cache/node-gyp/16.19.0/include/node/v8.h: In instantiation of ‘v8::Local<T>::Local(v8::Local<S>) [with S = v8::Value; T = v8::String]’:
../source/index.cc:48:53: required from here
/home/matthias/.cache/node-gyp/16.19.0/include/node/v8.h:211:42: error: static assertion failed: type check
211 | static_assert(std::is_base_of<T, S>::value, "type check");
| ^~~~~
/home/matthias/.cache/node-gyp/16.19.0/include/node/v8.h:211:42: note: ‘std::integral_constant<bool, false>::value’ evaluates to false
make: *** [addon.target.mk:111: Release/obj.target/addon/source/index.o] Error 1
make: Leaving directory '/home/matthias/projects/client/build'
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/lib/node_modules/node-gyp/lib/build.js:203:23)
gyp ERR! stack at ChildProcess.emit (node:events:513:28)
gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:293:12)
gyp ERR! System Linux 5.15.88-2-lts
gyp ERR! command "/home/matthias/.volta/tools/image/node/16.19.0/bin/node" "/usr/bin/node-gyp" "build"
gyp ERR! cwd /home/matthias/projects/client
gyp ERR! node -v v16.19.0
gyp ERR! node-gyp -v v9.3.1
gyp ERR! not ok
The addon file:插件文件:
#include <node.h>
namespace main
{
using v8::Array;
using v8::Context;
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
const char *ToCString(Isolate *isolate, Local<String> str)
{
String::Utf8Value cvalue(isolate, str);
return *cvalue ? *cvalue : "<string conversion failed>";
}
void Method(const FunctionCallbackInfo<Value> &args)
{
Isolate *isolate = args.GetIsolate();
char *fingerprint = const_cast<char *>(ToCString(isolate, args[0]));
char *server = const_cast<char *>(ToCString(isolate, args[2]));
char *remotes = const_cast<char *>(ToCString(isolate, args[3]));
}
void Init(Local<Object> exports)
{
NODE_SET_METHOD(exports, "myFunction", Method);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
} // namespace main
I had to leave out some parts because of confidentiality.由于保密原因,我不得不省略一些部分。
The macro NODE_SET_METHOD
is expanded to the inline function宏NODE_SET_METHOD
扩展为内联 function
inline void NODE_SET_METHOD(v8::Local<v8::Template> recv, const char *name,
v8::FunctionCallback callback)
It's first parameter type is v8::Local<v8::Template>
.它的第一个参数类型是v8::Local<v8::Template>
。 You attempt to pass export
of the type v8::Local<v8::Object>
.您尝试传递v8::Local<v8::Object>
类型的export
。 This is a reason of the assertion "type check" failure.这是断言“类型检查”失败的原因。
It's not enough context to make a fix.没有足够的上下文来进行修复。
You pass wrong Init
to NODE_MODULE
.您将错误的Init
传递给了NODE_MODULE
。 The first one should be第一个应该是
void Init(v8::Local<v8::Object> exports, v8::Local<v8::Value> module, void* priv);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.