Home > OS >  fatal error: opencv2/opencv.hpp: No such file or directory #include "opencv2/opencv.hpp"
fatal error: opencv2/opencv.hpp: No such file or directory #include "opencv2/opencv.hpp"

Time:07-02

I am new to C and OpenCV and I am stuck on this problem. I am using VS Code version 1.68.1 and when I try to run a main.cpp file, I always get this error:

main.cpp:2:10: fatal error: opencv2/opencv.hpp: No such file or directory 

    2 | #include "opencv2/opencv.hpp"
      |          ^~~~~~~~~~~~~~~~~~~~

This is the main.cpp code:

#include <stdio.h>
#include "opencv2/opencv.hpp"

using namespace cv;
int main(int argc, char** argv )
{

    Mat image;
    image = imread("lenna.jpg");
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

My CMakelists.txt file:

cmake_minimum_required(VERSION 3.0.0)
project(opencvTest)

include(CTest)
enable_testing()

set("OpenCV_DIR" "C:\\opencv4")
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable(opencvTest main.cpp)
target_link_libraries(opencvTest ${OpenCV_LIBS} )

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g  .exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g  .exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-ID","C:\\opencv4\\install\\include",
                "-LD","C:\\opencv4\\install\\x64\\mingw\\bin",                
                "-llibopencv_calib3d411",
                "-llibopencv_core411",
                "-llibopencv_dnn411",
                "-llibopencv_features2d411",
                "-llibopencv_flann411",
                "-llibopencv_highgui411",
                "-llibopencv_imgcodecs411",
                "-llibopencv_imgproc411",
                "-llibopencv_ml411",
                "-llibopencv_objdetect411",
                "-llibopencv_photo411",
                "-llibopencv_stitching411",
                "-llibopencv_video411",
                "-llibopencv_videoio411"
            ],
            "options": {
                "cwd": "C:\\msys64\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:/msys64/mingw64/bin/gcc.exe"
        }
    ]
}

launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g  .exe build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "g  .exe build active file"
    }
  ]
}

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\opencv4\\install\\include",
                "C:\\opencv4\\install\\x64\\mingw\\lib"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/msys64/mingw64/bin/g  .exe",
            "cStandard": "c11",
            "cppStandard": "c  17",
            "intelliSenseMode": "clang-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

What I noticed is that the definitions of the header point to this location for some reason

path being run but all my header files are in the opencv4 location. I do not know what to do about this and I have spent the last couple of hours trying to find a solution. Any help would be appreciated.

CodePudding user response:

Since we don't have that much information, I will just explain how include directories in CMake works.

When you do #include "opencv2/opencv.hpp", you are telling it to look in the provided include_directories() path ../include/opencv2/opencv.hpp where ../ are the previous directories. So the problem can be the directories path, improper installation of OpenCV, or because find_package() look for the packages in windows' package registry and that results in a different include path. So you can try to replace ${OpenCV_INCLUDE_DIRS} in

include_directories( ${OpenCV_INCLUDE_DIRS} )

with the full path to the folder named "include" that contains the file opencv.hpp. Note that doing it this way would likely require you to specify the library file path of the OpenCV library, which would include file name and the extension.

target_link_libraries("C:/Example/libs/example.lib")

example CMakeLists.txt file:

cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)


project("EXAMPLE" VERSION 0.1 LANGUAGES CXX)


message("CMake is running for windows version")
file(GLOB_RECURSE SRC_FILES src/*.cpp)  #the /* made it a comment lol*/

add_executable("${PROJECT_NAME}" ${SRC_FILES})

target_include_directories("${PROJECT_NAME}" PUBLIC
    "${CMAKE_SOURCE_DIR}/Dependencies/Example/include"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/include"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2_image-2.0.5/include"
)

target_link_libraries("${PROJECT_NAME}"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/lib/x64/SDL2.lib"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/lib/x64/SDL2main.lib"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/lib/x64/SDL2test.lib"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2_image-2.0.5/lib/x64/SDL2_image.lib"
)

CodePudding user response:

I was able to get the code working on Microsoft visual studio following the instructions from here: https://hackeradam.com/post/install-opencv4-windows10/

It was really clear and it should help anyone that encounters the same problem.

  • Related