I created a small program to test OpenCV C++ with Cmake and visual studio code but the build failed because of undefined functions and I don't know why Here is my code and my CmakeList.txt I have already set the variable paths like this :
C:\opencv\build\x64\vc15\bin
C:\opencv\build\x64\vc15\lib
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
Mat img = imread("lena.jpg");
imshow("IMAGE", img);
waitKey(0);
return 0;
}
cmake_minimum_required(VERSION 3.0.0)
project(TP1 VERSION 0.1.0)
include(CTest)
enable_testing()
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(TP1 main.cpp)
target_link_libraries(TP1 ${OpenCV_LIBS})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Build output using cmake tools in vscode
[main] Building folder: TP1
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/amine/Desktop/My_Studies/OpenCV/TP1/build --config Debug --target all -- -j 10
[build] Consolidate compiler generated dependencies of target TP1
[build] [ 50%] Building CXX object CMakeFiles/TP1.dir/main.cpp.obj
[build] [100%] Linking CXX executable TP1.exe
[build] CMakeFiles\TP1.dir/objects.a(main.cpp.obj): In function `main':
[build] C:/Users/amine/Desktop/My_Studies/OpenCV/TP1/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/amine/Desktop/My_Studies/OpenCV/TP1/main.cpp:10: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/Users/amine/Desktop/My_Studies/OpenCV/TP1/main.cpp:11: undefined reference to `cv::waitKey(int)'
[build] CMakeFiles\TP1.dir/objects.a(main.cpp.obj): In function `cv::Mat::~Mat()':
[build] C:/opencv/build/include/opencv2/core/mat.inl.hpp:751: undefined reference to `cv::fastFree(void*)'
[build] CMakeFiles\TP1.dir/objects.a(main.cpp.obj): In function `cv::Mat::release()':
[build] C:/opencv/build/include/opencv2/core/mat.inl.hpp:863: undefined reference to `cv::Mat::deallocate()'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\TP1.dir\build.make:115: TP1.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:370: CMakeFiles/TP1.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:120: all] Error 2
[build] Build finished with exit code 2
Location of opencv
C://opencv
Location of my code sample
Desktop//My_studies//OpenCV//TP1