简体   繁体   English

如何为node.js创建本机模块

[英]How to create native module for node.js

I want to create my own native Nodejs module. 我想创建自己的本机Nodejs模块。 I have been following http://nodejs.org/docs/latest/api/addons.html and try to understand how to create native module . 我一直在关注http://nodejs.org/docs/latest/api/addons.html并尝试了解如何创建本机模块。

First created hello.cc 首先创建了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(Handle<Object> exports) {
  exports->Set(String::NewSymbol("hello"),
  FunctionTemplate::New(Method)->GetFunction());
}

    NODE_MODULE(hello, init)

Then created binding.gyp 然后创建binding.gyp

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

Then I ran these command 然后我运行了这些命令

node-gyp configure
node-gyp build

finally created hello.js 最后创建了hello.js.

     var addon = require('./build/Release/hello');
      console.log(addon.hello()); 

At this time I got error like 这时我得到了错误

 module.js:355
 Module._extensions[extension](this, filename);

and so on. 等等。

I tried to build your example but I don't achieve to complete because of V8 versions. 我试图建立你的例子,但由于V8版本我无法完成。

So I followed the nodejs addon example again. 所以我再次关注nodejs addon示例 Today the example is a little different at yours. 今天的例子与你的有点不同。 And all works right. 一切正常。

My environment is the following: 我的环境如下:

  • SO: Ubuntu 14.04.2 LTS SO:Ubuntu 14.04.2 LTS
  • nodejs v0.12.7 nodejs v0.12.7
  • node-gyp v2.0.2 node-gyp v2.0.2
  • Python 2.7.6 Python 2.7.6
  • gcc 4.8.4 gcc 4.8.4
  • CPU Architecture: x86_64 CPU架构:x86_64
  • CPU op-mode(s): 32-bit, 64-bit CPU操作模式:32位,64位
  • Byte Order: Little Endian 字节顺序:Little Endian

I hope this will be useful. 我希望这会有用。

I guess your example comes from following this tutorial, problem is that I get several compiler errors that seems to be due to missing imports on the current release of node : 我想你的例子来自教程,问题是我得到了几个编译器错误,这些错误似乎是由于当前版本的node上缺少导入:

../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"));

Node version is 节点版本是

macbookproloreto:native admin$ node -v
v4.2.6

while node-gyp is node-gyp

macbookproloreto:native admin$ node-gyp -v
v3.3.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM