I'm trying to create own c++ addon to node from code that was written by my collegue.
It compiles to file.node
and then it crashes when I try to use it in node.
I have tried to prebuild library, then use library.dylib
and also to build it together by node-gyp.
Both of these approaches compile and throw an error on runtime.
What else can I do?
I'm working on OSX Mojave.
I've checked:
How to include c++ libraries so that node-gyp can link?
dyld: lazy symbol binding failed
Whole error:
dyld: lazy symbol binding failed: Symbol not found:
__ZN3mds7computeERNSt3__16vectorINS1_IdNS0_9allocatorIdEEEENS2_IS4_EEEE
Referenced from: /.../node_folder/build/release/file.node
Expected in: flat namespace
My gyp file:
{
"targets": [
{
"target_name": "name",
"sources": ["file.cc"],
"include_dirs": [
"<!(node -e \"require('nan')\")",
"/path/to/cpp/src/"
],
"link_settings": {
"libraries": ["-L/path/to/dylib/directory"]
},
"libraries": ["-L/path/to/dylib/directory"]
}
]
}
My package.json
{
...
"dependencies": {
"nan": "^2.12.1",
"node-gyp": "^3.8.0"
},
"scripts": {
"compile": "node-gyp rebuild",
"start": "node index.js"
},
"gypfile": true
}
My binding file:
#include <nan.h>
#include <iostream>
#include <my_header_file.h>
using namespace v8;
NAN_METHOD(compute)
{
if (!info[0]->IsArray())
{
Nan::ThrowTypeError("Argument myst be an array");
return;
}
...
std::vector<std::vector<double>> vector;
... (filling the vector with data)
//static std::vector<std::vector<double>> compute(std::vector<std::vector<double>> & distances_matrix);
mds::compute(vector);
}
NAN_MODULE_INIT(Initialize)
{
NAN_EXPORT(target, compute);
}
NODE_MODULE(addon, Initialize);
I see you are importing your header at #include <my_header_file.h>
.
If you are calling a method from your custom class for your NAN_METHOD you need to call it inline or else the compiler will not know where to look.
Run "c++filt (missing symbol)" to demangle and see where you need to call it
Example
Instead of method()
Use Class::method()
Your missing symbol demangled is mds::compute(std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > >&)
I have solved from https://github.com/nodejs/node-gyp/issues/1380,
The missing symbol's name indicates it's not using C linkage.
I just add extern "C" in the header file
extern "C" double add(double a ,double b);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With