OpenCV 4 + Windows + Mingw + VSCode

Installing OpenCV 4 on Linux is difficult, but can be done. On Windows it took me hours to get it working.



  1. install opencv
    • extract opencv[…].zip
    • copy to D:\OpenCV\
  2. install vscode
    • VSCodeUserSetup-[...].exe
  3. install cpp-tools in vscode
    • ctrl + shift + x
    • search for: “c++”
    • install “c/c++” from Microsoft
  4. install mingw
    • mingw-w64-install.exe – install to D:\mingw\ (IMPORTANT: don’t use spaces in name)
    • options: (version: 8.1.0, arch: x86_64, threads: posix, exception: seh, build rev: 0)
  5. set envoirment variables
    • change any paths in set_env.bat if you haven’t used mine
    • right click set_env.bat
    • select run as admin
  6. Finished 😉

Important notes

  • compiling: add “411” or corresponding opencv version to linker g++ ... -lopencv_core411
  • don’t use spaces in folder names

Compile OpenCV code

g++ -g test.cpp -o test.exe -I"%OPENCV_DIR%include" -L"%OPENCV_DIR%x64\mingw\bin" -lopencv_stitching411 -lopencv_objdetect411 -lopencv_calib3d411 -lopencv_features2d411 -lopencv_highgui411 -lopencv_videoio411 -lopencv_imgcodecs411 -lopencv_video411 -lopencv_photo411 -lopencv_ml411 -lopencv_imgproc411 -lopencv_flann411 -lopencv_core411


Big thanks to @huihut for providing pre-compiled OpenCV + mingw Windows builds. Building this myself would have been an additional nightmare.


Author: Anton Bracke

Software engineering student from Germany. Loves programming in PHP, JS and sometimes C.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.