Search code examples
c++importincludelibrariesvcpkg

Installing package via vcpkg causing a "opencv2/opencv.hpp: No such file or directory" error


I have installed vcpkg and opencv using vcpkg to try import the opencv library in c++ and I keep getting the following error:

main.cpp:2:10: fatal error: opencv2/opencv.hpp: No such file or directory
    2 | #include <opencv2/opencv.hpp>

This is my simple main.cpp:

#include <iostream>
#include <opencv2/opencv.hpp>       //I have also tried using "" instead

using namespace cv;                 //I have tried not using this line

int main(int, char**) {
    std::cout << "Hello, world!\n";
}

I have tested to see whether this was a problem with opencv, but when I installed and tried importing the booster library via vcpkg, the same issue persists.

I have a windows 10, and I am currently using VScode as my main editor, although I don't think it's a VScode problem because it doesn't matter whether use the run package in atom, vscode, or windows terminal, cygwin, or powershell the same error pops up.

I have tried many things including uninstalling and reinstalling vcpkg, adding the library, and bin files to the path in system environment variables, reinstalling everything in my C: drive instead of my D: drive, using cmake and even changing the #include statement to the following variations:

#include <opencv2>
#include <opencv>

However, I noted when I used the following import statement instead:

#include <C:\Users\nick-\DEV\opencv\build\include\opencv2\opencv.hpp>

I got this error:

C:\Users\nick-\DEV\opencv\build\include\opencv2\opencv.hpp:48:10: fatal error: opencv2/opencv_modules.hpp: No such file or directory
   48 | #include "opencv2/opencv_modules.hpp"

Which originated in one of the files of the opencv library.

I'm using vcpkg because I had previously tried using cmake and the issue wasn't resolved, the other StackOverflow link is [here][1] in case it's somewhat helpful. (I tried what was suggested and I found and moved the opencv library to the right address and it didn't help)

EDIT: Minimal reproducible example as per @Alan Birtles's suggestion, These are the steps that I did:

  • Install vcpkg by git cloning the [vcpkg repo][2]
  • Run the bootstrap command (.\vcpkg\bootstrap-vcpkg.bat)
  • installed opencv via vcpkg (vcpkg install opencv)
  • integrate vcpkg packages globally (vcpkg integrate install)
  • Created a main file (main.cpp) which you see above, I have made slight edits to it to make it minimal.
  • Then I compiled main.cpp (g++ main.cpp -o main) and got the error Then I tried the above troubleshooting steps (as well as tons of others I can't remember) and remained with the issue at hand.

EDIT: Attempted reincorporating cmake Following @Genjutsu's suggestion in the comment, I have done the following steps to re-setup cmake and get it working (it hasn't worked)

  • Installed the cmake, cmake tools, and cmake integration extensions on vscode
  • Run "cmake configure" as an executable
  • Added a handful of lines to my CMakeLists.txt file which I have shown below
cmake_minimum_required(VERSION 3.0.0)
project(fpc VERSION 0.1.0)

include(CTest)
enable_testing()

set(CMAKE_TOOLCHAIN_FILE "C:/Users/nick-/DEV/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(CMAKE_PREFIX_PATH "C:/Users/nick-/DEV/vcpkg/installed/x64-windows")

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(fpc main.cpp)

target_link_libraries(fpc ${OpenCV_LIBS})

message("OpenCV_INCLUDE_DIRS: " ${OpenCV_INCLUDE_DIRS})
message("OpenCV_LIBS: " ${OpenCV_LIBS})

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

Which give me the following output:

[main] Configuring folder: floorplan pathfinder cpp 
[cmake] Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19043.
[cmake] OpenCV_INCLUDE_DIRS: C:/Users/nick-/DEV/opencv/build/include
[cmake] OpenCV_LIBS: opencv_calib3dopencv_coreopencv_dnnopencv_features2dopencv_flannopencv_gapiopencv_highguiopencv_imgcodecsopencv_imgprocopencv_mlopencv_objdetectopencv_photoopencv_stitchingopencv_videoopencv_videoioopencv_world
[cmake] Configuring done
[cmake] Generating done
  • And finally I performed the suggested actions to use vcpkg with cmake under "Visual Studio Code with CMake Tools", and set my workplace's settings.json to this:
{
    "cmake.configureSettings": {
        "CMAKE_TOOLCHAIN_FILE": "D:/Program Files/vcpkg/scripts/buildsystems/vcpkg.cmake"
    }
}

EDIT: Added a manifest file (and also edited the cmake file) So I added a vcpkg.json manifest file to the best of my abilities, which is shown below. And modified my CMakeLists.txt file with changes reflected above.

{
    "name": "example",
    "version-string": "0.0.1",
    "dependencies": [
      "OpenCV"
    ]
  }

Solution

  • Simply said:
    a) VS Code is not affect by vcpkg integrate install, only MSBuild is ...
    b) setting the CMAKE_TOOLCHAIN_FILE only works before the very first project() call
    c) setting CMAKE_TOOLCHAIN_FILE before project() via cmake.configureSettings or other means does not work if there is already a configured cmake cache. d) don't modify CMAKE_PREFIX_PATH

    So starting from a clean cmake build without any cache: Set CMAKE_TOOLCHAIN_FILE before project() by any means you want and it works. You might also need to set VCPKG_TARGET_TRIPLET. (using #include <opencv2/opencv.hpp> seems fine)