4

I am creating a C++ addon and I wanted to use a static library. i have a .a library libarith which does simple addition.

-libarith.a
-hello.cc
-hello.js

my binding.gyp file is as follows:-

{ "targets": [
        {
        "target_name": "addon",
        "sources": [ "hello.cc" ],
        "libraries": [ "-/home/folder/api/addon/libarith.a" ],
        "cflags!": [ "-fno-exceptions" ],
        "cflags": [ "-std=c++11" ],
        "cflags_cc!": [ "-fno-exceptions" ]
        }
        ]
    }

When i compile my hello.cc it compiles well. But when i run my addon , it gives following error:

node: symbol lookup error: /home/folder/api/addon/build/Release/addon.node: undefined symbol: _ZN4demo3sumEii.

I am new to addons, a help would be very much appreciated.

Code Snippet:- libarith.a contains:

int sum(int a ,int b){

    return a+b;
} 

// hello.cc

#include <node.h>
#include <vector>
#include <iostream>
#include <v8.h>
using namespace std;
using namespace v8;
namespace demo {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void newmethod(const FunctionCallbackInfo<Value>& args)
{ extern int sum(int,int);

Isolate* isolate = args.GetIsolate();
double abc= sum(4,5);
Local<Number> newnumber =Number::New(isolate,abc);
v8::String::Utf8Value r(args[1]);
    std::string rst(*r);
Local<String> first = String::NewFromUtf8(isolate, "firstargument");
Local<String> second = String::NewFromUtf8(isolate, "secondargument");
Local<Object> newobj= Object::New(isolate);
newobj->Set(first,String::NewFromUtf8(isolate, *s));
newobj->Set(second,newnumber);
args.GetReturnValue().Set(newobj);

}
void init(Local<Object> exports) {
  NODE_SET_METHOD(exports, "newmethod", newmethod);
}
NODE_MODULE(addon, init)
} 

//hello.js

const addon = require('./build/Release/addon');

var ss = "helloo";
var samplestring = "It is not a sample string";
console.log(addon.newmethod(samplestring, ss));

EDIT:- The solution worked as follows. I tried to create a separate directory for the libraries and it worked fine.

Amit Nagar
  • 185
  • 1
  • 2
  • 9
  • libarith.a contains: int sum(int a ,int b){ return a+b; } // hello.cc #include #include #include #include using namespace std; using namespace v8; namespace demo { using v8::FunctionCallbackInfo; using v8::Isolate; using v8::Local; using v8::Object; using v8::String; using v8::Value; void newmethod(const FunctionCallbackInfo& args) { extern int sum(int,int); Isolate* isolate = args.GetIsolate(); double abc= sum(4,5); Local newnumber =Number::New(isolate,abc); – Amit Nagar Sep 05 '17 at 13:21
  • I have attached the code snippet. Hope it will be useful. – Amit Nagar Sep 05 '17 at 13:32

2 Answers2

3

It says, it cannot find the declaration(implementation of .h). I think you give wrong direction to your library. There are two solutions:

  1. Write full directory to your library at binding.gyp ~/whereItIsLocated, in my case it was ~/CLionProjects/node-player-core/PlayerCore/lib/x64/yourLibraryName.a
  2. If previous solution couldn't help, you can copy you library to /usr/lib. You can do it with sudo cp ~/whereLibraryLocated /usr/lib.
Jasurbek Nabijonov
  • 1,607
  • 3
  • 24
  • 37
2

Specify the .so library used for appropriate .h file in the binding.gyp file helps me to solve same issue:

"libraries": [
  "/usr/lib/mylib.so"
]

This links help me to found it: Getting "Symbol Lookup Error" when calling C library from C++ (Node.js Addon)

Alex
  • 1,297
  • 1
  • 16
  • 12
  • This one worked for me. I was trying to link libpulse to my addon, but adding `'-lpulse'` to `'cflags'` or `'cflags_cc'` in binding.gyp was not working, still resulting in a symbol lookup error. what ended up working was `'libraries': [ '/usr/lib/x86_64-linux-gnu/libpulse.so' ]`. I found the .so file by luck with the command `find . -name "libpulse.so"`. There should be more documentation on this process. – martian17 Jul 13 '22 at 15:37