0

I am very new to C++ and cmake (that means: I started yesterday) and I am stuck trying to include OpenCV. It might be a complete rookie mistake that I made somewhere during installation as well, so I am really hoping for your ideas and experience. So, this is the code I am trying to run:

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

int main(){
    std::cout << CV_VERSION << std::endl;
    return 0;
}

This is the error message I am getting:

main.cpp:1:30: fatal error: opencv2/opencv.hpp: No such file or directory
 #include <opencv2/opencv.hpp>

I had a look at this post here and updated my CMake file to this:

cmake_minimum_required(VERSION 3.0.0)
project(myproject VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)

include(CTest)
enable_testing()

find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
message("OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")

add_executable(myproject main.cpp)

target_link_libraries(myproject ${OpenCV_LIBS} )

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

UPDATE: Meanwhile, I added a couple of json files, as suggested here.But I am not exactely sure if I adjusted them correctly to my case.

So this is my c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:/opencv/sources/include",
                "C:/opencv/build/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/msys64/mingw64/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

But that didn't solve the problem either. Any ideas on what is wrong? As I said, I am completely new to this so it might be something really easy/stupid. ^^°

Those are the locations on my PC where Cmake and openCV are stored, in case that is relevant :

C:\opencv
C:\Program Files\CMake
5
  • Check the value of OpenCV_INCLUDE_DIRS and make sure it points to where you expect and that it contains opencv2/opencv.hpp Commented Jun 22, 2023 at 15:38
  • @AlanBirtles Thanks for that. I checked and I found two things: OpenCV_INCLUDE_DIRS = C:/opencv/build/include which seems to be fine, because there is this file C:\opencv\build\include\opencv2\opencv.hpp BUT there is also a file with the same name here: C:\opencv\sources\include\opencv2\opencv.hpp and now I am confused which one I need and how to link it properly... Commented Jun 23, 2023 at 6:34
  • the next thing to check is what the compiler commands actually are, if you're using make you can run make VERBOSE=1 Commented Jun 23, 2023 at 7:13
  • @AlanBirtles I added some information, maybe that helps? Commented Jun 26, 2023 at 8:25
  • c_cpp_properties.json only configures intellisense, it won't fix compiler errors Commented Jun 26, 2023 at 8:27

1 Answer 1

1

Ok, meanwhile I found a way to make it work. I originally followed this tutorial to set everything up. But upon running the script using the “play button” I encountered the error mentioned in the title and the description, even though the files were in the indicated location and all paths were correctly included. And I still cannot do it this way but there is another way and maybe there are more beginners like me who are not aware of this, which is why I want to share it.

In Windows, press SHIFT+CRTL+P and type in “CMake” in the window that pops up and you will find the CMake: Build command. Click it and it will generate an executable file for you, which in my case was located in build/Debug/. If you run this executable from the console, it works (provided your code doesn’t contain any errors).

It is kind of a workaround, because at least according to the mentioned tutorial there should be a simpler way to run the code, but at least I can continue working now.

It would still be interesting to know why it doesn’t find the files when I run the script directly in VSCode, so if anyone has an idea, please share it, it will help me and others that are struggling with the same (beginner?) problems.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.