nodejs c++扩展之hello world

8 篇文章 0 订阅
5 篇文章 0 订阅

环境 ubuntu 15.04

node v0.10.25

官网例子:

1.$ mkdir hellocc

2.把官网copy3个文件到hellocc下

创建文件

binding.gyp:

{
  "targets": [
    {
      "target_name": "hello",
      "sources": [ "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("hello,world"));
}

void init(Handle<Object> exports) {
  exports->Set(String::NewSymbol("hello"),
      FunctionTemplate::New(Method)->GetFunction());
}

NODE_MODULE(hello, init)

test.js:

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

console.log(addon.hello());

3.运行 node-gyp configure build
4.运行node test.js–>hello,world

说明:在编译的时候可能会发生各种问题,比如如下错误代码:

[ 'BUILDTYPE=Release', '-C', 'build' ]
  CXX(target) Release/obj.target/hello/hello.o
../hello.cc:6:28: error: ‘Arguments’ does not name a type
 Handle<Value> Method(const Arguments& args) {
                            ^
In file included from /home/bluesky/.node-gyp/4.0.0/include/node/node.h:42:0,
                 from ../hello.cc:1:
/home/bluesky/.node-gyp/4.0.0/include/node/v8.h: In function ‘v8::Handle<v8::Value> Method(const int&)’:
/home/bluesky/.node-gyp/4.0.0/include/node/v8.h:885:13: error: ‘v8::HandleScope::HandleScope()’ is protected
   V8_INLINE HandleScope() {}
             ^
../hello.cc:7:15: error: within this context
   HandleScope scope;
               ^
../hello.cc:8:16: error: ‘class v8::HandleScope’ has no member named ‘Close’
   return scope.Close(String::New("hello,world"));
                ^
../hello.cc:8:22: error: ‘Newis not a member of ‘v8::String’
   return scope.Close(String::New("hello,world"));
                      ^
../hello.cc: In function ‘void init(v8::Handle<v8::Object>)’:
../hello.cc:12:16: error: ‘NewSymbol’ is not a member of ‘v8::String’
   exports->Set(String::NewSymbol("hello"),
                ^
../hello.cc:13:35: error: no matching function for call to ‘v8::FunctionTemplate::New(v8::Handle<v8::Value> (&)(const int&))’
       FunctionTemplate::New(Method)->GetFunction());
                                   ^
../hello.cc:13:35: note: candidate is:
In file included from /home/bluesky/.node-gyp/4.0.0/include/node/node.h:42:0,
                 from ../hello.cc:1:
/home/bluesky/.node-gyp/4.0.0/include/node/v8.h:4349:34: note: static v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::Isolate*, v8::FunctionCallback, v8::Local<v8::Value>, v8::Local<v8::Signature>, int)
   static Local<FunctionTemplate> New(
                                  ^
/home/bluesky/.node-gyp/4.0.0/include/node/v8.h:4349:34: note:   no known conversion for argument 1 from ‘v8::Handle<v8::Value>(const int&) {aka v8::Local<v8::Value>(const int&)}’ to ‘v8::Isolate*’
../hello.cc: In function ‘v8::Handle<v8::Value> Method(const int&)’:
../hello.cc:9:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
hello.target.mk:88: recipe for target 'Release/obj.target/hello/hello.o' failed
make: *** [Release/obj.target/hello/hello.o] Error 1
make: Leaving directory '/home/bluesky/node-space/hellocc/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/home/bluesky/nvm/versions/node/v4.0.0/lib/node_modules/node-gyp/lib/build.js:270:23)
gyp ERR! stack     at emitTwo (events.js:87:13)
gyp ERR! stack     at ChildProcess.emit (events.js:172:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
gyp ERR! System Linux 3.19.0-15-generic
gyp ERR! command "/home/bluesky/nvm/versions/node/v4.0.0/bin/node" "/home/bluesky/nvm/versions/node/v4.0.0/bin/node-gyp" "configure" "build"
gyp ERR! cwd /home/bluesky/node-space/hellocc
gyp ERR! node -v v4.0.0
gyp ERR! node-gyp -v v3.0.3
gyp ERR! not ok

这个问题是node版本与gcc对应导致的,我之前安装node v4.0.0,一直 未能成功,后来重新装个node v0.10.25,一次就编译成功了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值