dimanche 5 juillet 2020

Node addon module with OpenCV4 C++

I am trying to reuse my C++ (OpenCV 4) program in my web app. To do that I am trying to compile a very basic C++ OpenCV code which just returns the version of the OpenCV. But I am facing some compilation issues shown below.

For reference here is my code repository: https://github.com/KishoreKaushal/nodeopencv

Given below is my code which I want to compile as a node add-on module.

#include <node.h>
#include <bits/stdc++.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

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

    string s = "OpenCV version : ";
    s += CV_VERSION ;

    v8::Local<v8::String> version = 
            v8::String::NewFromUtf8(isolate, s.c_str(), v8::String::kNormalString);

    args.GetReturnValue().Set(version);

}

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

    double a = 3.14159, b = 2.718;

    for (int i = 0; i < 100000; ++i) {
        a += b;
    }

    auto total = v8::Number::New(isolate, a);

    args.GetReturnValue().Set(total);
}


// this exports your custom function just like your module.exports in JS
void Initialize(v8::Local<v8::Object> exports) {
    NODE_SET_METHOD(exports, "sum", Sum);
    NODE_SET_METHOD(exports, "print_opencv_version", print_opencv_version);
}

NODE_MODULE(addon, Initialize)

This is my binding.gyp file:

{
    "targets" : [
        {
            "target_name" : "addon",
            "cflags" : ["`pkg-config --cflags opencv4`", '-std=c++11'],
            "link_settings" : {
                "libraries" : ["`pkg-config --libs opencv4`"]
            },
            "sources" : ["addon.cc"]
        }
    ]
}

And I am compiling it using this command: node-gyp clean configure build --verbose

And I am getting the following error:

gyp verb `which` succeeded for `make` /usr/bin/make
gyp info spawn make
gyp info spawn args [ 'V=1', 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory '/home/kaushal/Documents/git/nodeopencv/build'
  g++ -o Release/obj.target/addon/addon.o ../addon.cc '-DNODE_GYP_MODULE_NAME=addon' '-DUSING_UV_SHARED=1' 
  '-DUSING_V8_SHARED=1' '-DV8_DEPRECATION_WARNINGS=1' '-DV8_DEPRECATION_WARNINGS' 
  '-DV8_IMMINENT_DEPRECATION_WARNINGS' '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-D__STDC_FORMAT_MACROS' 
  '-DOPENSSL_NO_PINSHARED' '-DOPENSSL_THREADS' '-DBUILDING_NODE_EXTENSION' -I/home/kaushal/.cache/node-gyp/12.18.2/include/node -I/home/kaushal/.cache/node-gyp/12.18.2/src -I/home/kaushal/.cache/node-gyp/12.18.2/deps/openssl/config 
  -I/home/kaushal/.cache/node-gyp/12.18.2/deps/openssl/openssl/include -I/home/kaushal/.cache/node-gyp/12.18.2/deps/uv/include 
  -I/home/kaushal/.cache/node-gyp/12.18.2/deps/zlib -I/home/kaushal/.cache/node-gyp/12.18.2/deps/v8/include  
  -fPIC -pthread -Wall -Wextra -Wno-unused-parameter -m64 `pkg-config --cflags opencv4` -std=c++11 -O3 
  -fno-omit-frame-pointer -fno-rtti -fno-exceptions -std=gnu++1y -MMD -MF ./Release/.deps/Release/obj.target/addon/addon.o.d.raw   -c
In file included from /usr/local/include/opencv4/opencv2/flann/params.h:35,
                 from /usr/local/include/opencv4/opencv2/flann/flann_base.hpp:42,
                 from /usr/local/include/opencv4/opencv2/flann.hpp:48,
                 from /usr/local/include/opencv4/opencv2/opencv.hpp:65,
                 from ../addon.cc:10:
/usr/local/include/opencv4/opencv2/flann/any.h: In member function ‘virtual const std::type_info& cvflann::anyimpl::typed_base_any_policy<T>::type()’:
/usr/local/include/opencv4/opencv2/flann/any.h:60:71: error: cannot use ‘typeid’ with ‘-fno-rtti’
   60 |     virtual const std::type_info& type() CV_OVERRIDE { return typeid(T); }
      |                                                                       ^
/usr/local/include/opencv4/opencv2/flann/any.h: In member function ‘T& cvflann::any::cast()’:
/usr/local/include/opencv4/opencv2/flann/any.h:276:39: error: cannot use ‘typeid’ with ‘-fno-rtti’
  276 |         if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
      |                                       ^
/usr/local/include/opencv4/opencv2/flann/any.h: In member function ‘const T& cvflann::any::cast() const’:
/usr/local/include/opencv4/opencv2/flann/any.h:285:39: error: cannot use ‘typeid’ with ‘-fno-rtti’
  285 |         if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
      |                                       ^
/usr/local/include/opencv4/opencv2/flann/any.h: In member function ‘bool cvflann::any::empty() const’:
/usr/local/include/opencv4/opencv2/flann/any.h:293:59: error: cannot use ‘typeid’ with ‘-fno-rtti’
  293 |         return policy->type() == typeid(anyimpl::empty_any);
      |                                                           ^
/usr/local/include/opencv4/opencv2/flann/any.h: In member function ‘bool cvflann::any::has_type()’:
/usr/local/include/opencv4/opencv2/flann/any.h:313:42: error: cannot use ‘typeid’ with ‘-fno-rtti’
  313 |         return policy->type() == typeid(T);
      |                                          ^
In file included from /usr/local/include/opencv4/opencv2/flann/flann_base.hpp:43,
                 from /usr/local/include/opencv4/opencv2/flann.hpp:48,
                 from /usr/local/include/opencv4/opencv2/opencv.hpp:65,
                 from ../addon.cc:10:
/usr/local/include/opencv4/opencv2/flann/saving.h: In function ‘cvflann::IndexHeader cvflann::load_header(FILE*)’:
/usr/local/include/opencv4/opencv2/flann/saving.h:115:63: error: exception handling disabled, use ‘-fexceptions’ to enable
  115 |         throw FLANNException("Invalid index file, cannot read");
      |                                                               ^
../addon.cc: In function ‘void print_opencv_version(const v8::FunctionCallbackInfo<v8::Value>&)’:
../addon.cc:22:82: warning: ‘static v8::Local<v8::String> v8::String::NewFromUtf8(v8::Isolate*, const char*, v8::String::NewStringType, int)’ is deprecated: Use maybe version [-Wdeprecated-declarations]
   22 |             v8::String::NewFromUtf8(isolate, s.c_str(), v8::String::kNormalString);
      |                                                                                  ^
In file included from /home/kaushal/.cache/node-gyp/12.18.2/include/node/v8-internal.h:14,
                 from /home/kaushal/.cache/node-gyp/12.18.2/include/node/v8.h:27,
                 from /home/kaushal/.cache/node-gyp/12.18.2/include/node/node.h:67,
                 from ../addon.cc:8:
/home/kaushal/.cache/node-gyp/12.18.2/include/node/v8.h:3032:21: note: declared here
 3032 |       Local<String> NewFromUtf8(Isolate* isolate, const char* data,
      |                     ^~~~~~~~~~~
/home/kaushal/.cache/node-gyp/12.18.2/include/node/v8config.h:328:3: note: in definition of macro ‘V8_DEPRECATED’
  328 |   declarator __attribute__((deprecated(message)))
      |   ^~~~~~~~~~
../addon.cc:22:82: warning: ‘static v8::Local<v8::String> v8::String::NewFromUtf8(v8::Isolate*, const char*, v8::String::NewStringType, int)’ is deprecated: Use maybe version [-Wdeprecated-declarations]
   22 |             v8::String::NewFromUtf8(isolate, s.c_str(), v8::String::kNormalString);
      |                                                                                  ^
In file included from /home/kaushal/.cache/node-gyp/12.18.2/include/node/v8-internal.h:14,
                 from /home/kaushal/.cache/node-gyp/12.18.2/include/node/v8.h:27,
                 from /home/kaushal/.cache/node-gyp/12.18.2/include/node/node.h:67,
                 from ../addon.cc:8:
/home/kaushal/.cache/node-gyp/12.18.2/include/node/v8.h:3032:21: note: declared here
 3032 |       Local<String> NewFromUtf8(Isolate* isolate, const char* data,
      |                     ^~~~~~~~~~~
/home/kaushal/.cache/node-gyp/12.18.2/include/node/v8config.h:328:3: note: in definition of macro ‘V8_DEPRECATED’
  328 |   declarator __attribute__((deprecated(message)))
      |   ^~~~~~~~~~
In file included from ../addon.cc:8:
../addon.cc: At global scope:
/home/kaushal/.cache/node-gyp/12.18.2/include/node/node.h:608:43: warning: cast between incompatible function types from ‘void (*)(v8::Local<v8::Object>)’ to ‘node::addon_register_func’ {aka ‘void (*)(v8::Local<v8::Object>, v8::Local<v8::Value>, void*)’} [-Wcast-function-type]
  608 |       (node::addon_register_func) (regfunc),                          \
      |                                           ^
/home/kaushal/.cache/node-gyp/12.18.2/include/node/node.h:642:3: note: in expansion of macro ‘NODE_MODULE_X’
  642 |   NODE_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
      |   ^~~~~~~~~~~~~
../addon.cc:49:1: note: in expansion of macro ‘NODE_MODULE’
   49 | NODE_MODULE(addon, Initialize)
      | ^~~~~~~~~~~
make: *** [addon.target.mk:113: Release/obj.target/addon/addon.o] Error 1
make: Leaving directory '/home/kaushal/Documents/git/nodeopencv/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/lib/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:315:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Linux 5.4.0-40-generic
gyp ERR! command "/usr/bin/node" "/usr/bin/node-gyp" "clean" "configure" "build" "--verbose"
gyp ERR! cwd /home/kaushal/Documents/git/nodeopencv
gyp ERR! node -v v12.18.2
gyp ERR! node-gyp -v v7.0.0
gyp ERR! not ok 

Looks like there is some error related to -fno-rttii.

Does anyone have any idea of what is going on here?

Aucun commentaire:

Enregistrer un commentaire