2012-03-28 16 views
11

शामिल करने में सक्षम नहीं है मैं सी प्रोग्राम के साथ मिश्रित सी ++ द्वारा कस्टम नोड.जेएस एडन लिख रहा हूं।कस्टम node.js एडन बनाया जा सकता है लेकिन

addon.cc

#define BUILDING_NODE_EXTENSION 
#include <node.h> 
#include <node_buffer.h> 

using namespace v8; 
using namespace node; 


/* other logic and function... */ 


Handle<Value> RunCallback(const Arguments& args) { 
    HandleScope scope; 

    Local<Value> buffer1 = args[0]; 
    size_t size = Buffer::Length(buffer1->ToObject()); 
    char* bufferdata = Buffer::Data(buffer1->ToObject()); 

    /* some logic written in C style ... */ 

    Local<Function> cb = Local<Function>::Cast(args[1]); 
    const unsigned argc = 1; 
    Local<Value> argv[argc] = { Local<Value>::New(String::New(outputdata, outputSize)) }; 

    cb->Call(Context::GetCurrent()->Global(), argc, argv); 

    return scope.Close(Undefined()); 
} 

void Init(Handle<Object> target) { 
    target->Set(String::NewSymbol("runCallback"), FunctionTemplate::New(RunCallback)->GetFunction()); 
} 

NODE_MODULE(addon, Init) 

यह भी अन्य .cc फ़ाइलें शामिल की तरह कुछ के होते हैं, इसलिए WScript इस तरह है:

srcdir = '.' 
blddir = 'build' 
VERSION = '0.0.1' 

def set_options(opt): 
    opt.tool_options('compiler_cxx') 

def configure(conf): 
    conf.check_tool('compiler_cxx') 
    conf.check_tool('node_addon') 

def build(bld): 
    obj = bld.new_task_gen('cxx', 'shlib', 'node_addon') 
    obj.target = 'addon' 
    obj.source = ['addon.cc', 'otherFiles.cc'] 

जब मैं नोड waf कॉन्फ़िगर चलाने के लिए, यह दिखाता है:

Checking for program g++ or c++   : /usr/bin/g++ 
Checking for program cpp     : /usr/bin/cpp 
Checking for program ar     : /usr/bin/ar 
Checking for program ranlib    : /usr/bin/ranlib 
Checking for g++       : ok 
Checking for node path     : not found 
Checking for node prefix     : ok /usr/local 
'configure' finished successfully (0.169s) 

जब मैं नोड-वेफ बिल्ड चलाता हूं, तो यह दिखाता है:

Waf: Entering directory `/path/build' 
[ 1/25] cxx: addon.cc -> build/Release/addon_1.o 
... list of file ... 
build/Release/list of file -> build/Release/addon.node 
Waf: Leaving directory `/path/build' 
'build' finished successfully (0.544s) 

लेकिन जब मैं नोड आरईपीएल में निम्न प्रयास करें, यह पता चलता है:

var addon = require("./build/Release/addon"); 
Error: Unable to load shared library /path/build/Release/addon.node 
    at Object..node (module.js:472:11) 
    at Module.load (module.js:348:31) 
    at Function._load (module.js:308:12) 
    at Module.require (module.js:354:17) 
    at require (module.js:370:17) 
    at repl:1:13 
    at REPLServer.eval (repl.js:80:21) 
    at repl.js:190:20 
    at REPLServer.eval (repl.js:87:5) 
    at Interface.<anonymous> (repl.js:182:12) 

यह बहुत ही अजीब है। मैं जाँच की है कि फाइल सिस्टम वास्तुकला के साथ मेल खाना चाहिए:

$ file build/Release/addon.node 
build/Release/addon.node: Mach-O 64-bit bundle x86_64 

$ file `which node` 
/usr/local/bin/node: Mach-O 64-bit executable x86_64 

एनएम को देख कर, यह निम्न दिखाता है:

nm ./build/Release/addon.node 
0000000000011880 s GCC_except_table30 
0000000000001160 t _Init 
       U __Unwind_Resume_or_Rethrow 
       U ___bzero 
       U ___gxx_personality_v0 
0000000000013220 D _addon_module 
       U _free 
0000000000013600 D _lsfmeanTbl 
0000000000013420 D _memLfTbl 
       U _memcpy 
       U _memmove 
       U _puts 
       U _realloc 
0000000000011950 s _ssqEn_win.2272 
000000000001341c D _stMemLTbl 
00000000000132e0 D _state_frgqTbl 
00000000000132c0 D _state_sq3Tbl 
       U dyld_stub_binder 
       (... many are omitted ...) 

क्या संभावित कारण हो जाएगा? क्या मैं संकलित करने के लिए सी ++ फ़ाइलों के साथ सी फाइलों को गठबंधन नहीं कर सकता? क्या मुझे सभी malloc/realloc/free को हटा देना चाहिए? या किसी भी अन्य संभावित कारणों?

+0

मै मैक और लिनक्स पर निर्माण करने की कोशिश की है, परिणाम समान हैं, लाइब्रेरी समेत त्रुटि दिखा रहा है। –

+0

मुझे पूरा यकीन है कि समस्या बाहरी सी फाइलों के कारण है। पर क्यों? मुझे शक है। –

+2

शायद मुझे पूछना चाहिए, क्या node.js addon लिखने के लिए कोई शुद्ध सी समाधान है? –

उत्तर

0

Node.js का उपयोग करके जो संस्करण> = 0.7.6। समस्या सुलझ गई है। इसके अलावा, इसमें बहुत अधिक डीबग संदेश है।

0

लपेटें Init की परिभाषा() और extern "C" में NODE_MODULE मैक्रो का नाम सी ++ से बचने के लिए निर्यात किया प्रतीकों में से mangling:

extern "C" { 

void Init(Handle<Object> target) { 
    target->Set(String::NewSymbol("runCallback"), FunctionTemplate::New(RunCallback)->GetFunction()); 
} 

NODE_MODULE(addon, Init) 
} 
संबंधित मुद्दे