#include<stdio.h>
#include<opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv) {
Mat image;
image = imread("D:/pic.jpg");
if(!image.data)
{
printf("No image data found\n");
return(-1);
}
namedWindow("Display image",WINDOW_AUTOSIZE);
imshow("DIsplay Image",image);
waitKey(0);
return 0;
}
I am running this code on a Windows machine in vscode, with all my opencv and cmake files path in system environment path variable.I have set up my vscode for openCV from here.
[main] Building folder: test
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build d:/OpenCV_projects/test/build --config Debug --target all -j 14 --
[build] Consolidate compiler generated dependencies of target cmakefile
[build] [ 50%] Building CXX object CMakeFiles/cmakefile.dir/main.cpp.obj
[build] [100%] Linking CXX executable cmakefile.exe
[build] CMakeFiles\cmakefile.dir/objects.a(main.cpp.obj): In function `main':
[build] D:/OpenCV_projects/test/main.cpp:7: undefined reference to `cv::Mat::Mat()'
[build] D:/OpenCV_projects/test/main.cpp:8: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] D:/OpenCV_projects/test/main.cpp:8: undefined reference to `cv::Mat::operator=(cv::Mat&&)'
[build] D:/OpenCV_projects/test/main.cpp:8: undefined reference to `cv::Mat::~Mat()'
[build] D:/OpenCV_projects/test/main.cpp:14: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] D:/OpenCV_projects/test/main.cpp:15: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] D:/OpenCV_projects/test/main.cpp:16: undefined reference to `cv::waitKey(int)'
[build] D:/OpenCV_projects/test/main.cpp:7: undefined reference to `cv::Mat::~Mat()'
[build] D:/OpenCV_projects/test/main.cpp:8: undefined reference to `cv::Mat::~Mat()'
[build] D:/OpenCV_projects/test/main.cpp:7: undefined reference to `cv::Mat::~Mat()'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\cmakefile.dir\build.make:115: cmakefile.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:838: CMakeFiles/cmakefile.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:120: all] Error 2
[build] Build finished with exit code 2
New update: This is my cmakefile.txt.Ihave set up all this by a vedio tutorial.I think it's a issue of linking.
cmake_minimum_required(VERSION 3.0.0)
project(cmakefile VERSION 0.1.0)
include(CTest)
enable_testing()
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(cmakefile main.cpp)
target_link_libraries( cmakefile ${OpenCV_LIBS} )
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
UPDATE- I fixed the issue by installing (MSVC amd64) complier and again build my cmakefile and main.cpp files, now everthing is working fine.But i am not sure why mingw gcc complier is not working.