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

问题描述 投票:0回答:1

我一直在使用C ++代码进行工作,该代码从网络摄像头接收图像数据(灰度),并将检测到的角数(快速角)返回给javascript。为了成功编译代码,我下载并编译了opencv.js以使用emscripten创建每个库,对我自己的C ++类进行编码以获取图像数据,包括OpenCV.hpp和boom。效果非常好。

当我决定使用OpenCV FeatureDetector时,我的问题开始了。我能够编译仅包含标头的代码,但是一旦我从该模块中插入指令,就会出现链接错误。触发链接错误的代码行是:

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

我将OpenCV.js重新编译为WASM,问题仍然存在。我还运行了OpenCV.js测试,并且该模块中的所有组件在编译后都可以正常工作。

但是有一件奇怪的事:当我在编译的库上运行“ nm”命令时,出现了“ Bad section type”错误,即使使用该模块中的方法,即使OpenCV.js测试也没有显示错误。

我正在使用从源代码编译的OS X Catalina(10.15.3),OpenCV和Emscripten SDK,都是最新版本。

我的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);

}

我的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} )

链接错误:

[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

当我在opencvjs编译的lib上运行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)

谢谢你们的帮助。

javascript c++ opencv llvm emscripten
1个回答
0
投票

我知道了。感谢@ÖöTiib信息,我意识到build_js.py具有编译Zlib的标志。

在我的CMakeLists.txt中没有引用此库。所以我加了:

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

问题已解决;)

© www.soinside.com 2019 - 2024. All rights reserved.