0

I have a problem using OpenCV 4.1.2 in C++. I have this CMakelists.txt:

cmake_minimum_required(VERSION 2.8.12)
project( Barcode-cpp )
find_package( OpenCV REQUIRED )
add_compile_options(-std=c++11)
add_library( src
        src/VideoVeed.h
        src/VideoVeed.cpp
)
add_executable( program
            program/main.cpp
)
target_link_libraries( program
                    src
                    ${OpenCV_LIBS}
)

As you can see I have two folders with source code:

  • program contains main.cpp
  • src contains VideoVeed.h & VideoVeed.cpp

When I include OpenCV in main.cpp like this: <opencv2/opencv.hpp>, it works fine. But when I include OpenCV (the same way) it gives the error fatal error: 'opencv2/opencv.hpp' file not found.

I think I'm doing something wrong in my CMakelists.txt, but I can't figure out what exactly.

I hope someone is able to help me. Thanks in advance!

1 Answer 1

2

You should add the line, target_include_directories(), so that the OpenCV include directories are included in your executable:

add_executable( program
        program/main.cpp
)
target_include_directories(program PRIVATE ${OpenCV_INCLUDE_DIR})

Depending on the version of OpenCV you are using, you may need to use OpenCV_INCLUDE_DIRS instead:

add_executable( program
        program/main.cpp
)
target_include_directories(program PRIVATE ${OpenCV_INCLUDE_DIRS})

EDIT: OpenCV 4.1.2 populates the variable OpenCV_INCLUDE_DIRS, so this is the variable you should use. See this tutorial.

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

5 Comments

unfortunately this doesn't work... Where do I need to put it. Perhaps I placed it at the wrong place?
It should be placed after defining your program target, after the call to add_executable().
Hmm, this doesn;t work for me than... Thanks for trying tho!
I have OpenCV 4.1.2 btw, maybe that helps.
@mHvNG Yes, the version helps. I edited my answer, and it may help to clear your CMake cache and run CMake from scratch.

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.