4

I wrote a simple opencv project containing only a main.cpp file:

// main.cpp
#include <opencv/cv.h>
#include <opencv2/opencv.hpp>
int main()
{
    cv::Mat I = cv::imread("img.png");
    cv::imshow("img",I);
    cv::waitKey(0);
    return 0;
}

Then to build it, I wrote a CMakeLists.txt like this, which is actually copied from http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

In terminal, first I ran cmake . and a Makefile was generated. Then I ran make and got the following error:

fatal error: 
  'opencv2/opencv.hpp' file not found
#include <opencv2/opencv.hpp>
         ^
......

It seems the compiler cannot find OpenCV include dir. But I don't know where to add it to the CMakeLists.txt. My platform is Mac OS X 10.10, and OpenCV include and lib files have been installed at /usr/local/include and /usr/local/lib already. However, in Ubuntu, the compiler can find it and successfully build the project.

So what is a correct CMakeLists.txt in Mac OS X?

1
  • According to the error description, it seems that CMake have found OpenCV on your Mac (inclusion of file opencv/cv.h has been processed successfully). But either that OpenCV installation is too old, or not all OpenCV components are installed, so include file opencv2/opencv.hpp does not exist. You can check existence of both include files manually under /usr/local/include. Commented Aug 31, 2015 at 11:24

1 Answer 1

6

I finally actually read the header of /opt/local/share/OpenCV/OpenCVConfig.cmake installed via MacPorts. To use in external projects, it instructs to include:

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(MY_TARGET_NAME ${OpenCV_LIBS})

(simply adding include_directories to my CMakeLists.txt fixed this for me)

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.