Error linking OpenCV.js modules

24 views
Skip to first unread message

Marcos Trinca

unread,
Mar 6, 2020, 7:38:06 AM3/6/20
to emscripten-discuss
I've been working in a C++ Code which receives an image data (grayscale) from the webcam and return the number of corners detected (Fast Corners) to the javascript. To get success compiling the code I've download and compiled opencv.js to create each one of the libraries using emscripten, coded my own C++ class to get the image data, included OpenCV.hpp and boom. Works amazingly fine. 

My problem started when I decided to use OpenCV FeatureDetector. I'm able to compile the code just including the header <opencv2/features2d.hpp> but as soon as I insert an instruction from this module I got a link error.
The line of code triggering the link error is:

Ptr<FeatureDetector> detector = ORB::create();

I've recompiled OpenCV.js as WASM and the problem persists.
I've also ran the OpenCV.js tests and all of the components from this module are working fine after compiling.

There is one weird thing though: when I run the "nm" command on the compiled libs I got a "Bad section type" error, even the OpenCV.js tests shows no error using the methods from this module. 

My C++ code:

class MyCV {
    public:
        MTR(int width, int height) : 
            width(width), 
            height(height)
        {}

        int getWidth() const { return width; }
        void setWidth(int w) { width = w; }

        int getHeigth() const { return height; }
        void setHeight(int h) { height = h; }

        // get video pixels in grayscale from the heap
        int image_input(intptr_t buffer, size_t nSize) //query image input
        {
            int *imagePixels = reinterpret_cast<int*>(buffer);
            
            // convert to cv formatDigite o código aqui...

            Mat raw_data = cv::Mat(1, nSize, CV_8UC1, imagePixels);
            Mat img(640, 480, CV_8UC1, &raw_data);
            
            // detect keypoints
            std::vector<cv::KeyPoint> keypoints_fast;
            Ptr<FeatureDetector> detector = ORB::create(); // <<<---- Here I got the link error
            
            return 1;
        }

    private:
        int width;
        int height;
        
};

EMSCRIPTEN_BINDINGS(my_module) {
    class_<MyCV>("MyCV")
        .constructor<int, int>()
        .property("width", &MyCV::getWidth, &MyCV::setWidth)
        .property("height", &MyCV::getHeigth, &MyCV::setHeight)
        .function("image_input", &MyCV::image_input);

}


My CMakeLists.txt

set( CMAKE_CXX_STANDARD 11 )
cmake_minimum_required( VERSION 3.16 )
project( MyCV )

# Use C++ 11 by default
set( CMAKE_CXX_STANDARD 11 )

# Set Release as default build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif(NOT CMAKE_BUILD_TYPE)

file( GLOB opencv_include_modules "/Users/dev/Documents/_Git/opencv/modules/*/include" )
include_directories( ${opencv_include_modules} )
include_directories( /Users/dev/Documents/_Git/opencv/include )
include_directories( /Users/dev/Documents/_Git/opencv/build_wasm )

add_executable( MyCV src/MyCV.cc )

file( GLOB opencv_libs_wasm "/Users/dev/Documents/_Git/opencv/build_wasm/lib/*.a" )
target_link_libraries( MyCV ${opencv_libs_wasm} )

set(COMPILE_FLAGS "-Wno-missing-prototypes")
set_target_properties( MyCV PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS})

set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s WASM=1")
set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} --bind")
#set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s LLD_REPORT_UNDEFINED")

set_target_properties( MyCV PROPERTIES LINK_FLAGS ${EMSCRIPTEN_LINK_FLAGS} )

The link error:

[100%] Linking CXX executable MyCV.js
error: undefined symbol: gzclose
warning: Link with `-s LLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
error: undefined symbol: gzeof
error: undefined symbol: gzgets
error: undefined symbol: gzopen
error: undefined symbol: gzputs
error: undefined symbol: gzrewind
Error: Aborting compilation due to previous errors
shared:ERROR: '/Users/dev/Documents/_Git/emsdk/node/12.9.1_64bit/bin/node /Users/dev/Documents/_Git/emsdk/upstream/emscripten/src/compiler.js /var/folders/94/qc6kgl3x6r30g7y04t_9w4w00000gn/T/tmpDzc2sz.txt' failed (1)
make[2]: *** [MyCV.js] Error 1
make[1]: *** [CMakeFiles/MyCV.dir/all] Error 2
make: *** [all] Error 2

The message that comes from 'nm' command:

dev# nm libopencv_features2d.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(agast.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(agast_score.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(akaze.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(bagofwords.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(blobdetector.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(brisk.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(draw.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(dynamic.cpp.o) Unexpected metadata version: 2 (Expected: 1)
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(evaluation.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(fast.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(fast_score.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(feature2d.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(gftt.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(kaze.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(AKAZEFeatures.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(KAZEFeatures.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(fed.cpp.o) Unexpected metadata version: 2 (Expected: 1)
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(nldiffusion_functions.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(keypoint.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(main.cpp.o) Unexpected metadata version: 2 (Expected: 1)
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(matchers.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(mser.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(orb.cpp.o) Bad section type
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm: error: libopencv_features2d.a(opencl_kernels_features2d.cpp.o) Unexpected metadata version: 2 (Expected: 1)

Thank you guys for any help.

Marcos Trinca

unread,
Mar 6, 2020, 9:12:59 AM3/6/20
to emscripten-discuss
Fixed!

There was no reference to zlib in my CMakeLists.txt.
To fix the problem I added this line:

target_link_libraries(MyCV /Users/dev/Documents/_Git/opencv/build_wasm/3rdparty/lib/libzlib.a)

Thanks for Öö Tiib which gave me the tip about zlib in stack overflow.
Reply all
Reply to author
Forward
0 new messages