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!
cmake_minimum_required(VERSION 2.8) project( DisplayImage ) set(OpenCV_DIR /home/lmk/opencv-3.1.0/release) include_directories( /usr/local/opencv-3.1.0/include ) add_executable( DisplayImage DisplayImage.cpp ) target_link_libraries( DisplayImage /usr/local/opencv-3.1.0/lib )
. But new error exists:DisplayImage.cpp:(.text+0x34):undefined reference to ‘cv::imread(cv::String const&, int)’
– Little Tooth