1

I'm trying to build a node addon using the opencv library. I'm compiling it using node-gyp. I've achieved to link opencv using the binding.gyp config file, but I have some issues with the v8 library.

Here is my binding.gyp file:

    "targets": [
        {
            "target_name": "test",
            "sources": [ "src/native/test.cpp" ],
            "include_dirs": [
                "/usr/local/include/opencv4",
                "/usr/local/include/node",
                "/usr/include"
            ],
            "library_dirs": [ "/usr/local/lib", "/usr/lib" ],
            "libraries": [
                "/usr/local/lib/libopencv_core.so",
                "/usr/local/lib/libopencv_videoio.so",
                "/usr/local/lib/libopencv_highgui.so",
                "/usr/lib/libv8.so"
            ],
            "ldflags": [ "-Wl,-z,defs" ],
            "link_settings": {
                "libraries": [
                    "-lopencv_core",
                    "-lopencv_videoio",
                    "-lopencv_highgui",
                    "-lv8",
                    "-lpthread"
                 ]
            }
        }
    ]
}

Without linking opencv, I haven't any problem to build and run my addon, but as soon as I link opencv, the build failed because of v8:

test.cpp:(.text+0x1f9): undefined reference to `v8::V8::ToLocalEmpty()'
test.cpp:(.text+0x209): undefined reference to `v8::V8::FromJustIsNothing()'
test.cpp:(.text+0x219): undefined reference to `v8::V8::ToLocalEmpty()'
test.cpp:(.text+0x229): undefined reference to `v8::V8::ToLocalEmpty()'
test.cpp:(.text+0x239): undefined reference to `v8::V8::FromJustIsNothing()'
... and so on

As you can see in the binding.gyp file, I've tried to link the corresponding libraries and include for v8, but it doesn't make it.

I'm pretty sure it's a problem or a conflict with the configuration itself, because without linking opencv, the build works great and I've no problems to use the addon in a nodeJS file.

My c++ file is very simple, it's as simple as possible since I just want it to build and link libraries correctly:

#include <opencv4/opencv2/core.hpp>
#include <opencv4/opencv2/videoio.hpp>
#include <opencv4/opencv2/highgui.hpp>

#include <node/v8.h>
#include <node/node.h>
#include <iostream>
#include <stdio.h>

void capture(const v8::FunctionCallbackInfo<v8::Value>& args)
{
    v8::Isolate* isolate = v8::Isolate::GetCurrent();
    v8::HandleScope scope(isolate);

    cv::Mat frame;
    cv::VideoCapture cap;
}

void Initialize(v8::Local<v8::Object> exports) {
    NODE_SET_METHOD(exports, "capture", capture);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)

If you have any ideas about this build error, and why v8 isn't linking correctly when linking opencv, I would appreciate any help,

Thanks

Lucas Gras
  • 961
  • 1
  • 7
  • 22

1 Answers1

0

Well, why don't you link OpenCV statically, It licensed under Apache license. The Apache License does not place any restrictions on linking

rm -rf 3rdparty/opencv
mkdir -p 3rdparty/opencv
rm -rf tmp
mkdir tmp
cd tmp
rm -rf opencv-master
rm -rf opencv_contrib-master
git clone --branch 3.4 --depth 1 https://github.com/opencv/opencv.git opencv-master
git clone --branch 3.4 --depth 1 https://github.com/opencv/opencv_contrib.git opencv_contrib-master
mkdir build
cd build
cmake \
    -DCMAKE_INSTALL_PREFIX="$(pwd)/../../3rdparty/opencv\@3.4" \
    -DBUILD_SHARED_LIBS=OFF \
    -DENABLE_PRECOMPILED_HEADERS=YES \
    -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules \
    ../opencv-master
cmake --build .
make install

I created a simple script which will compile OpenCV 3.4 with opencv_contrib (SIFT, SURF available) for statically linking with Native Abstractions for Node.js.

{
    "targets": [
    {
        "target_name": "addon",
        "cflags": [
            "-std=c++11",
            "-stdlib=libc++"
        ],
        "cflags_cc!": [
            "-fno-rtti",
            "-fno-exceptions"
        ],
        "xcode_settings": {
            "GCC_ENABLE_CPP_RTTI": "YES",
            "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
        },
        "include_dirs": [
            "../../3rdparty/opencv/@3.4/include",
            "<!(node -e \"require('nan')\")"
        ],
        'libraries': [
            "<!@(node utils/find-libs.js)",
            "-framework OpenCL"
        ],
        "sources": [
            "./src/main.cc",
            "./src/lib/MainAddon.cc"
        ],
    }
  ]
}

The full source code is large enough so It published in this GitHub repository

Petr Tripolsky
  • 1,419
  • 15
  • 23