Step-by-Step Guide: Installing OpenCV C++ and Setting It Up in Visual Studio Code with CMake

Nicolai Nielsen · 140K views · 2021 · 15:48

Open on YouTube

Testing, debugging & developer toolsC, C++, Rust, Go & systems languagesComputer vision, speech & NLPtutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it as the setup tutorial they followed for OpenCV with CMake in VS Code, usually when asking about linker or configuration errors that came up while following it.

Recommended by 1 people across 1 different months, 2021 to 2023.

What it is

A 16-minute screen-recorded tutorial by Nicolai Nielsen on installing OpenCV for C++ on Windows and wiring it into Visual Studio Code using CMake. It covers installing OpenCV and CMake, adding the VS Code extensions, writing a CMakeLists.txt, and building a first image-display program.

What people said

Recommends Kyusv · 2023-01 · Stack Overflow · on “VSCode CMake Configuration Fail on Windows10”

I am a complete beginner in this stuff but i just had this issue and was able to solve it. I am not sure which one of the steps exactly helped me but here is everything i did that might have affected it: Uninstalled and reinstalled CMake choosing "Add CMake to the system PATH for all users" Installed Visual Studio 2022 c++ tool for windows Restarted my pc Added paths to opencv/build/x64/vc15/bin and lib and to the CMake's bin Created a cmakelist.txt by hand and copied contents from this video [this video] Reinstalled CMake extensions and downloaded c/c++ compile run extension(even though i already have code runner installed) With the DIY file crated in step 5 tried ctrl+shift+p and chose CMake co

Cites vivek uniyal · 2021-08 · Stack Overflow · on “Can not build my OpenCV project due to undefined references”

#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 [this video] . [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] Consol

Cites C4C · 2021-11 · Stack Overflow · on “Cmake build problem with opencv (error LNK2019) in vscode”

So I've been trying to get started on opencv c++. I followed a tutorial on how to set it up, link here [this video] . I wrote a simple opencv script for an image display. I built the project after that and I got this error: [build] main.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl cv::Mat::Mat(void)" (??0Mat@cv@@QEAA@XZ) referenced in function main [build] main.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl cv::Mat::~Mat(void)" (??1Mat@cv@@QEAA@XZ) referenced in function main [build] main.cpp.obj : error LNK2019: unresolved external symbol "public: class cv::Mat & __cdecl cv::Mat::operator=(class cv::Mat &&)" (??4Mat@cv@@QEAAAEAV01@$$QEAV01@@Z) refere

Cites CADSE · 2021-12 · Stack Overflow · on “How resolve the error find_package not providing in CMake”

I'm trying to learn OpenCV but for some reason I can't use the library, I'm using CMake but i don't know how to use and the code is 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 always give the error [cmake] CMake Error at CMakeLists.txt:7 (find_package): [cmake] By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has [cmake] asked CMake to find a pack

Cites ankanb312 · 2022-05 · Stack Overflow · on “Undefined reference to cv functions when using OpenCV with CMake”

I am following a tutorial ( [this video] ) 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::imre

Cites Chris Steffen · 2022-06 · Stack Overflow · on “Undefined reference when using OpenCV and CMake in VSCode”

Im trying to use OpenCV in VSCode and followed this videos instructions: [this video] Difference from the video is im using mingw compiler and im unable to get the msvc compiler (Not the issue though). main.cpp: #include <iostream> #include <opencv2/opencv.hpp> using namespace cv; int main() { std::cout << "aa" << std::endl; std::string img = "lenna.jpg"; Mat srcImage = imread(img); if (!srcImage.data) { return 1; } imshow("srcImage", srcImage); waitKey(0); return 0; } CMakeLists.txt: cmake_minimum_required(VERSION 3.0.0) project(OpenCV_Test VERSION 0.1.0) include(CTest) enable_testing() find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) add_executable(OpenCV_Test main.cpp)

Cites Crazy Engineer · 2023-06 · Stack Overflow · on “Setting up cmake with C++ and openCV: fatal error: opencv2/opencv.hpp: No such file or directory #include <opencv2/opencv.hpp>”

Ok, meanwhile I found a way to make it work. I originally followed this tutorial [this video] to set everything up. But upon running the script using the “play button” I encountered the error mentioned in the title and the description, even though the files were in the indicated location and all paths were correctly included. And I still cannot do it this way but there is another way and maybe there are more beginners like me who are not aware of this, which is why I want to share it. In Windows, press SHIFT+CRTL+P and type in “CMake” in the window that pops up and you will find the CMake: Build command. Click it and it will generate an executable file for you, which in my case was located in bui