I have been working on using a c library in my node project. After little bit investigation I found node-gyp.
I was successfully able to execute example but when I am trying to using third party c library functions in the code it was giving me linking error on run time.
Library can be found here http://bibutils.refbase.org/bibutils_3.40_src.tgz
I compiled the library independently to have *.a objects
I am using following example https://github.com/nodejs/node-addon-examples/tree/master/5_function_factory/node_0.12
So I have following questions as I can infer
Details related to script can be found below. bibutils folder is placed along with addon.cc
binding.gyp looks like
{
"targets": [
{
"target_name": "addon",
"sources": [ "addon.cc" ],
"include_dirs": ["bibutils/lib"],
"library_dirs": ["bibutils/lib/libbibutil.a","bibutils/lib/libbibprogs.a"]
}
]
}
modified addon.cc
#include <node.h>
#include "bibutils.h"
#include "bibprogs.h"
using namespace v8;
void MyFunction(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
/****This is not production code just to check the execution***/
bibl b;
bibl_init( &b );
bibl_free( &b );
/**************************************************************/
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world"));
}
void CreateFunction(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction);
Local<Function> fn = tpl->GetFunction();
// omit this to make it anonymous
fn->SetName(String::NewFromUtf8(isolate, "theFunction"));
args.GetReturnValue().Set(fn);
}
Compilation Result
user1@ubuntu:~/node-addon-examples/5_function_factory/node_0.12$ npm install
> function_factory@0.0.0 install /home/user1/node-addon-examples/5_function_factory/node_0.12
> node-gyp rebuild
make: Entering directory `/home/user1/node-addon-examples/5_function_factory/node_0.12/build'
CXX(target) Release/obj.target/addon/addon.o
SOLINK_MODULE(target) Release/obj.target/addon.node
COPY Release/addon.node
make: Leaving directory `/home/user1/node-addon-examples/5_function_factory/node_0.12/build'
On Execution
user1@ubuntu:~/node-addon-examples/5_function_factory/node_0.12$ node addon.js
node: symbol lookup error: /home/user1/node-addon-examples/5_function_factory/node_0.12/build/Release/addon.node: undefined symbol: _Z9bibl_initP4bibl
Debug Info:
user1@ubuntu:~/node-addon-examples/5_function_factory/node_0.12$ nm -C build/Release/addon.node | grep bibl_init
U bibl_init(bibl*)
The problem was communication between C++ and C. In above case a C header file was included in C++ code. Compile was expecting the C++ code. So on compiling linker was got choked due to mismatch in compiled code.
So I used the extern "C" directive to tell the compiler about C header files by following code.
extern "C" {
#include "bibutils.h"
#include "bibprogs.h"
}