0

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!

1
  • I have solved the error by modiflying the CMakeLists.txt : 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)’ Commented Mar 11, 2016 at 10:01

2 Answers 2

0

Why don't you use the 'typical' way, using find_package()?

Try to repeat the steps in this tutorial, and report what is happening.

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

1 Comment

find_package()? Can you tell me in details? I installed my opencv 3.1.0 in /usr/local/opencv-3.1.0. This is the first time I use cmake to compile.
0

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.

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.