Opencv C++ MinGW VSCode fatal error to compile

Here are instructions for setting up OpenCV in MSYS2/mingw64. I can't help with VsCode but perhaps this will help you make progress, or help other people who find this question by searching.


You don't need to actually build OpenCV yourself; there are prepackaged binaries for MinGW-w64. The purpose of MSYS2 is to provide a unix-style shell and act as a package manager. If you haven't used MSYS2 before:

  1. Install MSYS2 and update to latest as shown here.
  2. Open a MSYS2/mingw64 shell (not a MSYS2/msys2 shell). Binaries built in this shell will run as standalone Windows binaries, they do not depend on the MSYS2 environment.
  3. Test that g++ works.

Then you can add OpenCV:

  • pacman -Ss mingw64/mingw-w64-x86_64-opencv

and it's all ready to go. I compiled your sample program using the following command in MSYS2 shell:

g++ -o main main.cpp -std=c++17 -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc

and ran the program successfully.


Note that you do not need to use MSYS2 for the build environment; you can invoke g++ from Windows Command Prompt or from any other IDE if you set up the Include and Library paths to point to where MSYS2's package manager installed the headers and libraries. (Which is under /mingw64/include/opencv* and /mingw64/lib , under the MSYS2 installation root).


I have success trying to build and run a C++ program with OpenCV 3.4.1 using MinGW-w64 g++ in Visual Studio Code on Windows 10 x64. But I don't know if this is actually the "right" way to do it. Screenshot


  1. Download and Install MinGW-w64 choose x86_64-8.1.0-posix-seh-rt_v6-rev0 version or newer http://mingw-w64.org/doku.php/start.
  2. Open CMD and go to C:\ then clone this repo https://github.com/huihut/OpenCV-MinGW-Build then checkout the OpenCV-3.4.1-x64 branch or just execute this git clone https://github.com/huihut/OpenCV-MinGW-Build.git -b OpenCV-3.4.1-x64
  3. Add Mingw-w64 bin folder to the system path (For me the path is C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin)
  4. Add OpenCV mingw bin folder that you clone earlier to the system path (For me the path is C:\OpenCV-MinGW-Build\x64\mingw\bin)
  5. For now you can already build a program using g++ using CMD just type g++ test.cpp -IC:\OpenCV-MinGW-Build\include -LC:\OpenCV-MinGW-Build\x64\mingw\bin -llibopencv_calib3d341 -llibopencv_core341 -llibopencv_dnn341 -llibopencv_features2d341 -llibopencv_flann341 -llibopencv_highgui341 -llibopencv_imgcodecs341 -llibopencv_imgproc341 -llibopencv_ml341 -llibopencv_objdetect341 -llibopencv_photo341 -llibopencv_shape341 -llibopencv_stitching341 -llibopencv_superres341 -llibopencv_video341 -llibopencv_videoio341 -llibopencv_videostab341 just change the test.cpp to your .cpp file name.
  6. Open your .cpp file using Visual Studio Code then install Code Runner extension https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner, I use Code Runner to easily run my program.
  7. Press Ctrl + comma in Visual Studio Code then search for "code-runner.executorMap": and change the content on"cpp:" entry to "cd $dir && g++ $fileName -o $fileNameWithoutExt -IC:/OpenCV-MinGW-Build/include -LC:/OpenCV-MinGW-Build/x64/mingw/bin -llibopencv_calib3d341 -llibopencv_core341 -llibopencv_dnn341 -llibopencv_features2d341 -llibopencv_flann341 -llibopencv_highgui341 -llibopencv_imgcodecs341 -llibopencv_imgproc341 -llibopencv_ml341 -llibopencv_objdetect341 -llibopencv_photo341 -llibopencv_shape341 -llibopencv_stitching341 -llibopencv_superres341 -llibopencv_video341 -llibopencv_videoio341 -llibopencv_videostab341 && $dir$fileNameWithoutExt", you can apply this for user settings or just for workspace settings. `
  8. After that just press Ctrl+Alt+N to Build and Run your program or just press the play button.

I believe you can also apply this to the tasks.json by adding the -IC:\OpenCV-MinGW-Build\include .. to the args part to use the Visual Studio Code C/C++ extension debugging. Also you can add "C:/OpenCV-MinGW-Build/include" to the c_cpp_properties.json include path to enable intellisense. If you want to use another version of OpenCV just replace all the "341" part of the g++ argument to another version for example to use OpenCV 3.3.1 use -llibopencv_calib3d331 and so on.


EDIT: I have try to edit my tasks.json and launch.json and it works with gdb debugger. tasks.json : https://gist.github.com/agtbaskara/4a2ec9a3a9a963069e719c0477185321 launch.json : https://gist.github.com/agtbaskara/de04db8b6a31522dd1e62c43aa6e0f89 c_cpp_properties.json : https://gist.github.com/agtbaskara/0eb773ac3085557baf7bf20b031fb49e