Reputation: 11
I'm trying to run a C++ code for the first time. The documentation suggests downloading CMake to compile the project which also utilizes OpenCV. Right now, I ran $cmake . in the terminal inside the project directory and I ran $make and I'm getting 100% build. When I run $g++ Fotokite.cpp I get this error:
In file included from Fotokite.cpp:8:
./Fotokite.hpp:11:10: fatal error: 'opencv2/core.hpp' file not found
#include "opencv2/core.hpp"
^~~~~~~~~~~~~~~~~~
1 error generated.
Can anyone please help me understand what's going on? Is this something related to makefiles, opencv, or cmake?
Thank you
Upvotes: 1
Views: 1307
Reputation: 487
OpenCV ships with a CMake config file, this should be the preferred mechanism for including it. For example, you can include OpenCV using the modern CMake target-based approach for CMake as follows, for an example let's use the code from https://docs.opencv.org/master/db/df5/tutorial_linux_gcc_cmake.html
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
Now to include OpenCV we can use find_package
. The example CMake file below will work with the standard install location (built against OpenCV 4.5 for testing purposes). If you are building against a local build of OpenCV you can pass in the PATH option to find_package
with the install root of your local build. To include OpenCV you can use target_link_libraries to link against the targets produced by the OpenCV CMake config file during the install step and this will correctly manage all transitive dependencies of the target (include directories, compiler defines, compiler flags, link libraries, and equivalent fields for transitive dependencies of this target).
cmake_minimum_required(VERSION 3.18)
project( UseOpenCV )
find_package( OpenCV REQUIRED)
add_executable( UseOpenCV main.cpp )
target_link_libraries( UseOpenCV
PRIVATE
opencv_highgui
)
Upvotes: 1