Javascript 如何为node.js创建本机模块
我想创建自己的本机Nodejs模块。我一直在关注并试图理解如何创建本机模块 首先创建hello.ccJavascript 如何为node.js创建本机模块,javascript,node.js,node-gyp,Javascript,Node.js,Node Gyp,我想创建自己的本机Nodejs模块。我一直在关注并试图理解如何创建本机模块 首先创建hello.cc #include <node.h> #include <v8.h> using namespace v8; Handle<Value> Method(const Arguments& args) { HandleScope scope; return scope.Close(String::New("world")); } void init(H
#include <node.h>
#include <v8.h>
using namespace v8;
Handle<Value> Method(const Arguments& args) {
HandleScope scope;
return scope.Close(String::New("world"));
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
然后我运行这些命令
node-gyp configure
node-gyp build
终于创建了hello.js
var addon = require('./build/Release/hello');
console.log(addon.hello());
这时我犯了一个错误,比如
module.js:355
Module._extensions[extension](this, filename);
等等。我试图构建您的示例,但由于V8版本的原因,我无法完成 于是,我又跟着那条路走了。今天,你的例子有点不同。一切正常 我的环境如下:
- 所以:Ubuntu 14.04.2 LTS
- 节点JS v0.12.7
- 节点gyp v2.0.2
- Python 2.7.6
- 通用条款4.8.4
- CPU体系结构:x86_64
- CPU操作模式:32位、64位
- 字节顺序:小端
我希望这将是有用的。我猜您的示例来自以下教程,问题是我遇到了几个编译器错误,这些错误似乎是由于当前版本的
节点上缺少导入造成的:
../hello.cc:7:15: error: calling a protected constructor of class 'v8::HandleScope'
HandleScope scope;
^
/Users/admin/.node-gyp/4.2.6/include/node/v8.h:885:13: note: declared protected here
V8_INLINE HandleScope() {}
^
../hello.cc:8:16: error: no member named 'Close' in 'v8::HandleScope'
return scope.Close(String::New("world"));
~~~~~ ^
../hello.cc:8:30: error: no member named 'New' in 'v8::String'
return scope.Close(String::New("world"));
~~~~~~~~^
../hello.cc:13:29: error: cannot initialize a parameter of type 'v8::Isolate *' with an lvalue of type 'Handle<v8::Value> (const v8::internal::Arguments &)'
FunctionTemplate::New(Method)->GetFunction());
^~~~~~
/Users/admin/.node-gyp/4.2.6/include/node/v8.h:4350:16: note: passing argument to parameter 'isolate' here
Isolate* isolate, FunctionCallback callback = 0,
^
../hello.cc:12:23: error: no member named 'NewSymbol' in 'v8::String'
target->Set(String::NewSymbol("hello"),
~~~~~~~~^
In file included from ../hello.cc:1:
In file included from /Users/admin/.node-gyp/4.2.6/include/node/node.h:42:
/Users/admin/.node-gyp/4.2.6/include/node/v8.h:327:9: error: cannot initialize a member subobject of type 'v8::FunctionTemplate *' with an lvalue of type 'const char *'
: val_(that) {}
^ ~~~~
../hello.cc:8:34: note: in instantiation of function template specialization 'v8::Local<v8::FunctionTemplate>::Local<const char>' requested here
return scope.Close(String::New("world"));
而节点gyp
是
macbookproloreto:native admin$ node-gyp -v
v3.3.1
我还没试过,但你读过吗?没有,我会看的。谢谢您在引发错误的行中发布的错误,但实际上不包括错误消息本身,请更新它。谢谢。我去看看
macbookproloreto:native admin$ node -v
v4.2.6
macbookproloreto:native admin$ node-gyp -v
v3.3.1