如何修复'Undefined reference'错误OpenCV和G ++

时间:2019-10-04 17:48:21

标签: c++ windows opencv g++ mingw

我试图在使用OpenCV的VSCode中构建简单的程序,已经阅读了成千上万的帖子,并且它们都是在linux上制作的(并且我尝试在Windows上完成),这是我的代码

这是我尝试使用的:

"args": [
                "-g",
                "-I",
                "C:\\OpenCV\\opencv\\build\\include",
                "${file}",
                "-L",
                "C:\\OpenCV\\opencv\\build\\x64\\vc15\\lib",
                "-l",
                "opencv_world345",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],

我几次检查路径

#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], 1 );
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

这就是我得到的结果

  

执行任务:C:\ MinGW \ bin \ g ++。exe -IC:\ OpenCV \ opencv \ build \ include> d:\ programowanie \ Projekt \ Project \ main.cpp -L> C:\ OpenCV \ opencv \ build \ x64 \ vc15 \ lib -l opencv_world345 -o> d:\ programowanie \ Projekt \ Project \ main.exe << / p>      

c:/ mingw / bin /../ lib / gcc / mingw32 / 8.2.0 /../../../../ mingw32 / bin / ld.exe:> C:\ Users \ xxx \ AppData \ Local \ Temp \ ccXtHNOY.o:main.cpp :(。text + 0x72):对cv::imread(cv::String const&, int)' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: >C:\Users\xxx\AppData\Local\Temp\ccXtHNOY.o:main.cpp:(.text+0xe3): undefined reference to cv :: namedWindow(cv :: String const&,int)的未定义引用”   c:/ mingw / bin /../ lib / gcc / mingw32 / 8.2.0 /../../../../ mingw32 / bin / ld.exe:> C:\ Users \ xxx \ AppData \ Local \ Temp \ ccXtHNOY.o:main.cpp :(。text + 0x129):>对cv::imshow(cv::String const&, cv::_InputArray const&)' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: >C:\Users\xxx\AppData\Local\Temp\ccXtHNOY.o:main.cpp:(.text+0x149): >undefined reference to cv :: waitKey(int)的未定义引用   c:/ mingw / bin /../ lib / gcc / mingw32 / 8.2.0 /../../../../ mingw32 / bin / ld.exe:> C:\ Users \ xxx \ AppData \ Local \ Temp \ ccXtHNOY.o:main.cpp :(。text $ _ZN2cv6StringC1EPKc [__ ZN2cv6StringC1EPKc] + 0x42):未定义对cv::String::allocate(unsigned int)' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: >C:\Users\xxx\AppData\Local\Temp\ccXtHNOY.o:main.cpp:(.text$_ZN2cv6StringD1Ev[__ZN2cv6StringD1Ev]+0xf): undefined reference to cv :: String :: deallocate()'的引用   c:/ mingw / bin /../ lib / gcc / mingw32 / 8.2.0 /../../../../ mingw32 / bin / ld.exe:> C:\ Users \ xxx \ AppData \ Local \ Temp \ ccXtHNOY.o:main.cpp :(。text $ ZN2cv6StringaSERKS0 [__ ZN2cv6StringaSERKS0 _] + 0x1c):对cv::String::deallocate()' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: >C:\Users\xxx\AppData\Local\Temp\ccXtHNOY.o:main.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x2d): undefined reference to cv :: fastFree(void *)'的未定义引用/>   c:/ mingw / bin /../ lib / gcc / mingw32 / 8.2.0 /../../../../ mingw32 / bin / ld.exe:> C:\ Users \ xxx \ AppData \ Local \ Temp \ ccXtHNOY.o:main.cpp :(。text $ _ZN2cv3Mat7releaseEv [__ ZN2cv3Mat7releaseEv] + 0x40):未定义对cv::Mat::deallocate()' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: >C:\Users\xxx\AppData\Local\Temp\ccXtHNOY.o:main.cpp:(.text$_ZN2cv3MataSEOS0_[__ZN2cv3MataSEOS0_]+0xb4): undefined reference to cv :: fastFree(void *)'的引用   collect2.exe:错误:ld返回1退出状态

我不知所措,希望任何人都知道发生了什么事

1 个答案:

答案 0 :(得分:2)

问题是您正在使用Visual Studio二进制文件进行mingw。如果该库是c库,则可能有效,但不适用于opencv。以下站点具有用于mingw的非官方opencv二进制文件:https://github.com/huihut/OpenCV-MinGW-Build