google or-tools nodejs addon - node: symbol lookup error, undefined symbol with

341 Views Asked by At

i try to build a node addon and want to include the google or-tools.

with node-gyp configure build the addon compiles fine, but when i want to execute the script:

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

console.log(addon.hello());

i get the error node: symbol lookup error: /root/opt_02/build/Release/addon.node: undefined symbol: _ZN19operations_research8MPSolver12CreateSolverERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

i already moved the lib directory to /usr/lib, so my bindings.gyp looks as this:

{
  'targets': [{
    'target_name': 'addon',
    'sources': [
      'src/addon.cc'
    ],
    'cflags_cc': [
      '-std=c++17'
    ]
    'include_dirs': [
      '<!(node -p "require(\'node-addon-api\').include")',
      '/root/opt_02/dependencies/ortools/include'
    ],
  }
  ]
}

and the addon.cc looks like this (the code inside operations_research is copied from the google docs):

#include <node.h>

#include <memory>
#include <vector>

#include "ortools/linear_solver/linear_solver.h"

namespace operations_research {

    void BasicExample() {
        // Create the linear solver with the GLOP backend.
        std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("GLOP"));

        // Create the variables x and y.
        MPVariable* const x = solver->MakeNumVar(0.0, 1, "x");
        MPVariable* const y = solver->MakeNumVar(0.0, 2, "y");

        // Create a linear constraint, 0 <= x + y <= 2.
        MPConstraint* const ct = solver->MakeRowConstraint(0.0, 2.0, "ct");
        ct->SetCoefficient(x, 1);
        ct->SetCoefficient(y, 1);

        // Create the objective function, 3 * x + y.
        MPObjective* const objective = solver->MutableObjective();
        objective->SetCoefficient(x, 3);
        objective->SetCoefficient(y, 1);
        objective->SetMaximization();

        solver->Solve();

    }
}

namespace demo {

    using v8::FunctionCallbackInfo;
    using v8::Isolate;
    using v8::Local;
    using v8::Object;
    using v8::String;
    using v8::Value;

    void Method(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();

    operations_research::BasicExample();

    args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world").ToLocalChecked());
    }

    void Initialize(Local<Object> exports) {
    NODE_SET_METHOD(exports, "hello", Method);

    }
    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
}

Does anybody know to to resolve this error?

1

There are 1 best solutions below

1
Laurent Perron On

You need to compile with c++17 activated.