Reputation: 99
I am compiling opencv programs with cmake. The code is as follows:
DisplayImage.cpp:
#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;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
set(OpenCV_DIR /home/lmk/opencv-3.1.0/release)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
I put the DisplayImage.cpp and CMakeLists.txt in the folder called test in my personal home folder, namely /home/lmk/test/
.
Then I use command lines :
lmk@lmk-virtual-machine:~/test$ mkdir build
lmk@lmk-virtual-machine:~/test$ cd build
lmk@lmk-virtual-machine:~/test/build$ cmake ..
Which give me:
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lmk/test/build
But when I use:
Scanning dependencies of target DisplayImage
[100%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
/home/lmk/test/DisplayImage.cpp:2:30: fatal eror:opencv2/opencv.hpp:No such file or directory
compilation terminated.
make[2]: * [CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o] Error 1
make[1]: * [CMakeFiles/DisplayImage.dir/all] Error 2
make: *** [all] Error 1
Do you konw why? I am using opencv 3.1.0 and cmake 2.8.12.2 in the terminal of ubuntu 14.04 in VM!
Upvotes: 0
Views: 7598
Reputation: 20016
This question is a bit old, so I had to build OpenCV 2 from source:
$ git clone --depth 1 --branch 2.4.13.7 https://github.com/opencv/opencv.git
$ cmake -S opencv -B opencv-build -DCMAKE_BUILD_TYPE=Release -DWITH_CUDA=NO -DCMAKE_INSTALL_PREFIX=$PWD/opencv-install
$ cmake --build opencv-build
$ cmake --install opencv-build
Then I wrote the following CMakeLists.txt for your code:
cmake_minimum_required(VERSION 3.21)
project(test)
find_package(OpenCV 2 REQUIRED)
add_executable(DisplayImage DisplayImage.cpp)
target_link_libraries(DisplayImage PRIVATE ${OpenCV_LIBS})
To build this, I simply ran:
$ cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DOpenCV_ROOT=$PWD/opencv-install
...
$ cmake --build build/ --verbose
[1/2] /usr/bin/c++ -I/path/to/opencv-install/include/opencv -I/path/to/opencv-install/include -O3 -DNDEBUG -MD -MT CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o -MF CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o.d -o CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o -c /path/to/DisplayImage.cpp
[2/2] : && /usr/bin/c++ -O3 -DNDEBUG CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o -o DisplayImage -Wl,-rpath,/path/to/opencv-install/lib ../opencv-install/lib/libopencv_videostab.so.2.4.13 ../opencv-install/lib/libopencv_ts.a ../opencv-install/lib/libopencv_superres.so.2.4.13 ../opencv-install/lib/libopencv_stitching.so.2.4.13 ../opencv-install/lib/libopencv_contrib.so.2.4.13 -ldl -lm -lpthread -lrt -ldl -lm -lpthread -lrt ../opencv-install/lib/libopencv_nonfree.so.2.4.13 ../opencv-install/lib/libopencv_ocl.so.2.4.13 ../opencv-install/lib/libopencv_gpu.so.2.4.13 ../opencv-install/lib/libopencv_photo.so.2.4.13 ../opencv-install/lib/libopencv_objdetect.so.2.4.13 ../opencv-install/lib/libopencv_legacy.so.2.4.13 ../opencv-install/lib/libopencv_video.so.2.4.13 ../opencv-install/lib/libopencv_ml.so.2.4.13 ../opencv-install/lib/libopencv_calib3d.so.2.4.13 ../opencv-install/lib/libopencv_features2d.so.2.4.13 ../opencv-install/lib/libopencv_highgui.so.2.4.13 ../opencv-install/lib/libopencv_imgproc.so.2.4.13 ../opencv-install/lib/libopencv_flann.so.2.4.13 ../opencv-install/lib/libopencv_core.so.2.4.13 && :
If you have OpenCV installed in a standard system location, then -DOpenCV_ROOT=$PWD/opencv-install
is unnecessary.
Upvotes: 0
Reputation: 1632
Why don't you use the 'typical' way, using find_package()
?
Try to repeat the steps in this tutorial, and report what is happening.
Upvotes: 0