0

When I used VS Code to write my C++ code with OpenCV, I found that although the exe file is generated, the terminal will not show the output of my code. Here is the screenshot of my terminal.

Here's my code:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char** argv) {
  std::cout << "aa" << std::endl;

  std::string img = "lenna.jpg";
  Mat srcImage = imread(img);
  if (!srcImage.data) {
    return 1;
  }
  namedWindow("Display window", WINDOW_AUTOSIZE);
  imshow("Display window", srcImage);
  waitKey(0);
  return 0;
}

However, when I comment all the codes related with OpenCV (from std::string img = "lenna.jpg" to waitKey(0)), the problem disappears, and it just display "aa" normally.

How can I fix the problem?

  • Try running your exe by double-clicking it in the explorer. It will most probably complain about a missing DLL. Copy said dll from `D:/msys64/mingw64/bin` and place it next to your executable. – HolyBlackCat Jun 30 '22 at 13:15
  • I'm guessing it can't find the openCV Dll. Make sure its in the path environment variable,. – Code Gorilla Jun 30 '22 at 13:16
  • It just closed immediately after I opened the exe. It seems that `waitKey(0)` does not work. – 思晨_ Jun 30 '22 at 13:18
  • For the environment variable, I added `D:\coding\c_cpp\OpenCV-MinGW-Build-OpenCV-4.5.5-x64\x64\mingw\bin`. Is it enough? – 思晨_ Jun 30 '22 at 13:20
  • Fixed. I forget to add `include` folder to my environment variable – 思晨_ Jun 30 '22 at 23:43

0 Answers0