c++node.jsnode.js-nan

Postgres driver in NodeJS C++ Addon


I'm currently developing a C++ addon for NodeJS using the abstraction layer Nan. I would like to make a PostgreSQL request from this addon. But I get the following error:

   module.js:597
  return process.dlopen(module, path._makeLong(filename));
                 ^

Error: ....cpp/build/Release/MyModule.node: undefined symbol: _ZTVN4pqxx14connect_directE
    at Error (native)
    at Object.Module._extensions..node (module.js:597:18)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (...cpp/test.js:1:77)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10) 

Here is my binding.gyp:

{
  "targets": [
    {
      "target_name": "MyModule",
      "sources": [ "Main.cpp"],
      "include_dirs": [
        "<!(node -e \"require('nan')\")"
      ],
      "cflags": [
        "-fexceptions",
        "-lpq",
        "-lpqxx"
      ],
      "cflags_cc": [
        "-fexceptions",
        "-lpq",
        "-lpqxx"
      ]
    }
  ]
}

And my Main.cpp file

#include <nan.h>
#include <iostream>
#include <pqxx/pqxx>

using namespace Nan;
using namespace v8;
using namespace std;
using namespace pqxx;

NAN_METHOD(MyModule) {
        // Database connection
        connection C(
                    "dbname     = ... \
                     user       = ... \
                     password   = ... \
                     hostaddr   = 127.0.0.1 \
                     port       = 5432");        
}

NAN_MODULE_INIT(Init) {
        Nan::Set(target, New<String>("MyModule").ToLocalChecked(),
                 GetFunction(New<FunctionTemplate>(MyModule)).ToLocalChecked());
}

NODE_MODULE(parser, Init)

I'm stuck with this error and I didn't find any solution on the web. I would be nice if someone could help!


Solution

  • dlopen tries to load a shared library. However, this library misses a symbol, _ZTVN4pqxx14connect_directE. To decode it:

    $ c++filt _ZTVN4pqxx14connect_directE`
    vtable for pqxx::connect_direct
    

    Therefore, you need to link a library to your library (addon), which has this symbol. (The postgres driver?) Make sure your build system doesn't delay the linking, and the lib appears when your run ldd your_addon. If the driver is installed to a non-standard directory, you can experiment with setting LD_LIBRARY_PATH, as a temporary solution.