简体   繁体   English

使用 Emscripten 在我自己的 C++ 类上链接 OpenCV.js 模块时出错

[英]Error linking OpenCV.js modules on my own C++ class using Emscripten

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.我一直在使用 C++ 代码,它从网络摄像头接收图像数据(灰度)并将检测到的角数(快速角)返回给 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.为了成功编译代码,我下载并编译了 opencv.js 以使用 emscripten 创建每个库,编码我自己的 C++ 类以获取图像数据,包括 OpenCV.hpp 和繁荣。 Works amazingly fine.工作得非常好。

My problem started when I decided to use OpenCV FeatureDetector.当我决定使用 OpenCV FeatureDetector 时,我的问题就开始了。 I'm able to compile the code just including the header 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.我已将 OpenCV.js 重新编译为 WASM,但问题仍然存在。 I've also ran the OpenCV.js tests and all of the components from this module are working fine after compiling.我还运行了 OpenCV.js 测试,并且该模块中的所有组件在编译后都运行良好。

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.但是有一件奇怪的事情:当我在编译的库上运行“nm”命令时,我得到了一个“Bad section type”错误,即使 OpenCV.js 测试使用这个模块中的方法也没有显示错误。

I'm using OS X Catalina (10.15.3), OpenCV and Emscripten SDK compiled from source, both the latest versions.我使用的是从源代码编译的 OS X Catalina (10.15.3)、OpenCV 和 Emscripten SDK,都是最新版本。

My C++ code:我的 C++ 代码:

class MyCV {
    public:
        MyCV(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);

            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
            // etc ...

            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我的 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 when I run nm command on the opencvjs compiled lib:当我在 opencvjs 编译的库上运行 nm 命令时的消息:

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.谢谢你们的帮助。

I got it working.我让它工作了。 Thanks to @Öö Tiib info I realised build_js.py has a flag to compile Zlib.感谢@Öö Tiib info 我意识到 build_js.py 有一个标志来编译 Zlib。

There was no reference to this lib in my CMakeLists.txt.我的 CMakeLists.txt 中没有对这个库的引用。 So I added:所以我补充说:

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

Problem fixed ;)问题已解决;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM