【问题标题】:Unable to compile C++ code (having OpenCV usage) to wasm无法将 C++ 代码(使用 OpenCV)编译为 wasm
【发布时间】:2020-11-11 21:54:12
【问题描述】:

我有以下 C++ 代码。这里我是一些图像处理相关操作的OpenCV库。

#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <opencv/cv.hpp>

using namespace cv;
using namespace std;


int main()
{
    try
    {
        Mat frame, g_frame, r_frame; //, reduced_frame;
        int REDUCTION = 3;
        //--- INITIALIZE VIDEO_CAPTURE
        VideoCapture cap;

        // open selected camera using selected API
        cap.open(cv::CAP_DSHOW);
        // check if we succeeded
        if (!cap.isOpened()) {
            cerr << "ERROR! Unable to open camera\n";
            return -1;
        }

        //--- GRAB AND WRITE LOOP
        cout << "Start grabbing" << endl
             << "Press any key to terminate" << endl;

        // image_window win;
        while(true)
        {
            // wait for a new frame from camera and store it into 'frame'
            cap.read(frame);
            // check if we succeeded
            if (frame.empty()) {
                cerr << "ERROR! blank frame grabbed\n";
                break;
            }

            // flipping the image
            cv::flip(frame, frame, 1);
            cv::resize(frame, frame, cv::Size(), 2.0f, 2.0f, cv::INTER_CUBIC);

            // converting image to grey scale image
            cv::cvtColor(frame, g_frame, CV_BGR2GRAY);

            // reducing size of image
            cv::resize(g_frame, r_frame, cv::Size(), 1.0/REDUCTION, 1.0/REDUCTION, cv::INTER_CUBIC);

            cv::imshow("Live", frame);
            if( waitKey(10) == 27 ) break; // stop capturing by pressing ESC
        }

    }
    catch (exception& e)
    {
        cout << "\nexception thrown!" << endl;
        cout << e.what() << endl;
    }
    return 0;
}

当我尝试使用以下命令将此代码编译为 wasm 时:

emcc -msse3 -msimd128 -std=c++11 -O3 -I ../opencv/build/include main.cpp -lpthread -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 -s TOTAL_MEMORY=1024MB -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']" -s WASM=1 -o main.js

然后我收到以下错误:

error: undefined symbol: _ZN2cv12VideoCapture4openEi (referenced by top-level compiled C/C++ code)
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`
warning: __ZN2cv12VideoCapture4openEi may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv12VideoCapture4readERKNS_12_OutputArrayE (referenced by top-level compiled C/C++ code)
warning: __ZN2cv12VideoCapture4readERKNS_12_OutputArrayE may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv12VideoCaptureC1Ev (referenced by top-level compiled C/C++ code)
warning: __ZN2cv12VideoCaptureC1Ev may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv12VideoCaptureD1Ev (referenced by top-level compiled C/C++ code)
warning: __ZN2cv12VideoCaptureD1Ev may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv3Mat10deallocateEv (referenced by top-level compiled C/C++ code)
warning: __ZN2cv3Mat10deallocateEv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv4flipERKNS_11_InputArrayERKNS_12_OutputArrayEi (referenced by top-level compiled C/C++ code)
warning: __ZN2cv4flipERKNS_11_InputArrayERKNS_12_OutputArrayEi may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv6String10deallocateEv (referenced by top-level compiled C/C++ code)
warning: __ZN2cv6String10deallocateEv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv6String8allocateEm (referenced by top-level compiled C/C++ code)
warning: __ZN2cv6String8allocateEm may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv6imshowERKNS_6StringERKNS_11_InputArrayE (referenced by top-level compiled C/C++ code)
warning: __ZN2cv6imshowERKNS_6StringERKNS_11_InputArrayE may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv6resizeERKNS_11_InputArrayERKNS_12_OutputArrayENS_5Size_IiEEddi (referenced by top-level compiled C/C++ code)
warning: __ZN2cv6resizeERKNS_11_InputArrayERKNS_12_OutputArrayENS_5Size_IiEEddi may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv7waitKeyEi (referenced by top-level compiled C/C++ code)
warning: __ZN2cv7waitKeyEi may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv8cvtColorERKNS_11_InputArrayERKNS_12_OutputArrayEii (referenced by top-level compiled C/C++ code)
warning: __ZN2cv8cvtColorERKNS_11_InputArrayERKNS_12_OutputArrayEii may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv8fastFreeEPv (referenced by top-level compiled C/C++ code)
warning: __ZN2cv8fastFreeEPv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZNK2cv12VideoCapture8isOpenedEv (referenced by top-level compiled C/C++ code)
warning: __ZNK2cv12VideoCapture8isOpenedEv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors
emcc: error: 'C:/emsdk/node/12.18.1_64bit/bin/node.exe C:\emsdk\upstream\emscripten\src\compiler.js C:\Users\Nitin\AppData\Local\Temp\tmppq_ad1ya.txt' failed (1)

请帮帮我。

使用错误本身提供的建议解决了上述问题。

emcc -msse3 -msimd128 -std=c++11 -O3 -I ../opencv/build/include main.cpp -lpthread -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 -s TOTAL_MEMORY=1024MB -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']" -s WASM=1 -s LLD_REPORT_UNDEFINED -s ERROR_ON_UNDEFINED_SYMBOLS=0 -o main.html

但是,在浏览器中打开 main.html 文件时。我现在收到以下错误。

CompileError: WebAssembly.instantiate(): Compiling function #607 failed: invalid value type 'Simd128', enable with --experimental-wasm-simd @+105826

请帮我解决这个问题。

【问题讨论】:

  • 这与链接过程中缺少一些库有关(编译成功后)
  • 感谢您的建议,我已删除该错误,但现在代码出现另一个错误。请查看更新后的问题。

标签: opencv c++11 webassembly


【解决方案1】:

您需要遵循错误中给出的建议。检查错误的第 2 行和第 3 行。

【讨论】:

  • 感谢您的建议,我已删除该错误,但现在代码出现另一个错误。请查看更新后的问题。
【解决方案2】:

您必须使用选项 --experimental-wasm-simd 运行浏览器,因为 SIMD 还不是 webassembly 规范的一部分。

【讨论】:

  • 先生,要运行代码,我只需在浏览器中打开 main.html 文件。我该如何运行它??
  • 我不知道,Windows 上的惯用方式是什么,但在 linux 上,您可以从命令行启动它,例如 firefox --experimental-wasm-simd。在 Windows 上,您必须使用整个路径,例如 C:\\Program Files\Firefox\firefox.exe
猜你喜欢
  • 2015-07-23
  • 2020-11-04
  • 2019-10-12
  • 2020-11-04
  • 2017-09-27
  • 2014-10-17
  • 1970-01-01
  • 2020-01-22
  • 2012-04-01
相关资源
最近更新 更多