"undefined reference" when trying to compile OpenCV C++ with MinGW on VScode

124 Views Asked by At

I want to write an OpenCV program in C++ using Visual Studio Code. I've written a test program.

#include <iostream>
#include <cstdlib>
#include <opencv2\\opencv.hpp>

using namespace std;
using namespace cv;

int main(){
    Mat image = imread("image.png");
    
    while(true){
        imshow("live" , image);
        if(waitKey(0) == 'N') 
            break;
    }
    return 0;
}

this is the PATH of my environment variable

C:\msys64\mingw64\include\c++\13.2.0

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "OpenCV",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-I",
                "C:\\OpenCV-MinGW-Build-OpenCV-3.4.8-x64\\include",
                "-L",
                "C:\\OpenCV-MinGW-Build-OpenCV-3.4.8-x64\\x64\\mingw\\bin",
                "-llibopencv_calib3d348",
                "-llibopencv_core348",
                "-llibopencv_dnn348",
                "-llibopencv_features2d348",
                "-llibopencv_flann348",
                "-llibopencv_highgui348",
                "-llibopencv_imgcodecs348",
                "-llibopencv_imgproc348",
                "-llibopencv_ml348",
                "-llibopencv_objdetect348",
                "-llibopencv_photo348",
                "-llibopencv_stitching348",
                "-llibopencv_video348",
                "-llibopencv_videoio348"
            ],
            "group": "build"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ]
}

g++ .\test.cpp -o .\test.exe

When I run this command in the terminal, I get the following error.

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\oying\AppData\Local\Temp\ccbjLsFr.o:HelloWorld.cpp:(.text+0x3e): undefined reference to `cv::imread(cv::String const&, int)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\oying\AppData\Local\Temp\ccbjLsFr.o:HelloWorld.cpp:(.text+0x80): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\oying\AppData\Local\Temp\ccbjLsFr.o:HelloWorld.cpp:(.text+0xa2): undefined reference to `cv::waitKey(int)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\oying\AppData\Local\Temp\ccbjLsFr.o:HelloWorld.cpp:(.text$_ZN2cv6StringC1EPKc[_ZN2cv6StringC1EPKc]+0x51): undefined reference to `cv::String::allocate(unsigned long long)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\oying\AppData\Local\Temp\ccbjLsFr.o:HelloWorld.cpp:(.text$_ZN2cv6StringD1Ev[_ZN2cv6StringD1Ev]+0x14): undefined reference to `cv::String::deallocate()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\oying\AppData\Local\Temp\ccbjLsFr.o:HelloWorld.cpp:(.text$_ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\oying\AppData\Local\Temp\ccbjLsFr.o:HelloWorld.cpp:(.text$_ZN2cv3MatD1Ev[_ZN2cv3MatD1Ev]+0x39): undefined reference to `cv::fastFree(void*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\oying\AppData\Local\Temp\ccbjLsFr.o:HelloWorld.cpp:(.text$_ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
collect2.exe: error: ld returned 1 exit status

How can I resolve this? Do I need to write a Makefile or use CMake?

0

There are 0 best solutions below