1

I am following a tutorial (https://www.youtube.com/watch?v=m9HBM1m_EMU) for using OpenCV through CMake and I am having the following errors when I try to build using mingw-w64.

[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/bnick/Desktop/opencvtest/build --config Debug --target all -j 8 --
[build] Consolidate compiler generated dependencies of target opencvtest
[build] [ 50%] Linking CXX executable opencvtest.exe
[build] CMakeFiles\opencvtest.dir/objects.a(main.cpp.obj): In function `main':
[build] C:/Users/bnick/Desktop/opencvtest/main.cpp:8: undefined reference to `cv::Mat::Mat()'
[build] C:/Users/bnick/Desktop/opencvtest/main.cpp:9: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/Users/bnick/Desktop/opencvtest/main.cpp:9: undefined reference to `cv::Mat::operator=(cv::Mat&&)'
[build] C:/Users/bnick/Desktop/opencvtest/main.cpp:9: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/bnick/Desktop/opencvtest/main.cpp:15: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/Users/bnick/Desktop/opencvtest/main.cpp:16: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/Users/bnick/Desktop/opencvtest/main.cpp:17: undefined reference to `cv::waitKey(int)'
[build] C:/Users/bnick/Desktop/opencvtest/main.cpp:8: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/bnick/Desktop/opencvtest/main.cpp:9: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/bnick/Desktop/opencvtest/main.cpp:8: undefined reference to `cv::Mat::~Mat()'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\opencvtest.dir\build.make:115: opencvtest.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:838: CMakeFiles/opencvtest.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:120: all] Error 2
[build] Build finished with exit code 2

Here is the my CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(opencvtest VERSION 0.1.0)

include(CTest)
enable_testing()

find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable(opencvtest main.cpp)

target_link_libraries( opencvtest ${OpenCV_LIBS} )

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

And this is my main.cpp

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
int main(int argc, char** argv )
{

    Mat image;
    image = imread("C:/Users/kemik/OneDrive/Skrivebord/Images/lenna.png");
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}
ankanb312
  • 11
  • 1
  • Are you sure that the opencv you are using was compiled with mingw and not msvc? – drescherjm May 17 '22 at 16:03
  • @drescherjm I am sure since I am using VSC as my IDE which displays your build kit at the bottom. It says that the build kit is [GCC 8.1.0 x86_64-w64-mingw32] – ankanb312 May 17 '22 at 16:07
  • My advice is to upgrade your mingw by installing msys2 and have it install opencv for your mingw. The current gcc version is 11.2. Related: [https://stackoverflow.com/questions/30069830/how-to-install-mingw-w64-and-msys2](https://stackoverflow.com/questions/30069830/how-to-install-mingw-w64-and-msys2) – drescherjm May 17 '22 at 16:14
  • The documentation for VSCode even recommends using msys2: [https://code.visualstudio.com/docs/cpp/config-mingw](https://code.visualstudio.com/docs/cpp/config-mingw) – drescherjm May 17 '22 at 16:15
  • @drescherjm I have installed msys2 and I'm still receiving the same error. – ankanb312 May 17 '22 at 17:27
  • That is really odd. Did you install opencv using pacman in the mingw64 shell? – drescherjm May 17 '22 at 17:52
  • Yes, I have done that. I am trying to now run using the CMake extension on VSC and I am selecting the new compiler that I downloaded. – ankanb312 May 17 '22 at 19:34

0 Answers0