#include Statement not finding a correct path

Even though I copied the complete path and pasted it in the #include statement, building the file fails with: fatal error C1083: Cannot open include file: 'opencv2/core/hal/interface.h': No such file or directory. If I Right click on this include statement and select "Go to Document" it finds and loads the "interface.h" file into VS 2019 on my Windows 10 machine. The path is correct so I don't know why the failure! Suggestions???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  //Test program for OpenCV4.3.0

#include <C:\OpenCV430\build\include\opencv2\core\hal\interface.h>
#include <C:\OpenCV430\build\include\opencv2\core\cvdef.h>
#include <C:\OpenCV430\build\include\opencv2\core.hpp>
#include <C:\OpenCV430\build\include\opencv2\imgcodecs.hpp>
#include <C:\OpenCV430\build\include\opencv2\highgui.hpp>
#include <iostream>

using namespace cv;

int main()
{
    std::string image_path = samples::findFile("starry_night.jpg");
    Mat img = imread(image_path, IMREAD_COLOR);
    if (img.empty())
    {
        std::cout << "Could not read the image: " << image_path << std::endl;
        return 1;
    }
    imshow("Display window", img);
    int k = waitKey(0); // Wait for a keystroke in the window
    if (k == 's')
    {
        imwrite("starry_night.png", img);
    }
    return 0;
}
Here is how Visual Studio handles #include statements: https://docs.microsoft.com/en-us/cpp/preprocessor/hash-include-directive-c-cpp?view=vs-2019

Angle-bracket form
The preprocessor searches for include files in this order:

1) Along the path that's specified by each /I compiler option.

2) When compiling occurs on the command line, along the paths that are specified by the INCLUDE environment variable.

You should avoid having absolute paths in your #include code, because it makes your code not portable. You should rather have appropriate search path set up in your project settings/compiler flags.
Please see:
https://stackoverflow.com/a/15975661
https://stackoverflow.com/a/19415917
Last edited on
Thanks! I used the "absolute" path as prior build attempts also failed, so I figured it has to find the .h file with a complete path. I was wrong there too!
It looks likely that the OpenCV headers require a partial path?

1
2
3
4
5
6
#include <opencv2\core\hal\interface.h>
#include <opencv2\core\cvdef.h>
#include <opencv2\core.hpp>
#include <opencv2\imgcodecs.hpp>
#include <opencv2\highgui.hpp>
#include <iostream> 


Where you've added C:\OpenCV430\build\include to the include path

Andy
Last edited on
Thanks! Workin on it!
Topic archived. No new replies allowed.