Search code examples
c++opencvsublimetext3

SOLVED - "fatal error: opencv: no such file or directory" in Sublime Text for c++


I'm not having any success with #include <opencv>, no matter what I try. [EDIT: I've also tried #include <opencv2> and #include <opencv4>, but there was no difference].

For context, I'm on Ubuntu 20.04, using Sublime Text 3, the latest version of OpenCV is installed on the system (at /usr/local/include/opencv4/opencv2), and I've tried something like six different build systems for cpp, including the default.

no matter what I try and how I fiddle with files, I always get the same error:

/home/user/Projects/Mind/CScript/Script.cpp:5:10: fatal error: opencv: No such file or directory
    5 | #include <opencv>
      |          ^~~~~~~~
compilation terminated.
[Finished in 0.1s with exit code 1]
[shell_cmd: g++ "/home/user/Projects/Mind/CScript/Script.cpp" -o "/home/user/Projects/Mind/CScript/Script" && "/home/user/Projects/Mind/CScript/Script"]
[dir: /home/user/Projects/Mind/CScript]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

I even tried to copy the whole opencv folder into the file path, but there was no change at all.

EDIT: The solution that worked was to paste the opencv2 folder from /usr/local/include/opencv4/ to /usr/local/include/. Apparently that additional folder wasn't supposed to be there.


Solution

  • An #include refers to a file, not a folder. A file so specified is looked for in the list of include paths.

    -I /usr/local/include/opencv4/opencv2 adds that path to the list of include paths. The path, however, looks broken to me -- OpenCV has the legacy API in opencv/, and the updated API in opencv2/, but in either case there should be no opencv4/ in the path. (Double-checked with my APT.) Are you sure you did the OpenCV setup correctly?

    The actual header's name is opencv2/opencv.hpp (note the .hpp at the end; generally, only the standard library's includes are without .hpp, all the third party libs I have seen so far use the .hpp extension).

    Best practice is to leave the include path alone and write #include "opencv2/opencv.hpp" (with the subdirectory name) in your sources to avoid ambiguity.