1

I have two files:

test.cpp :

#include "highgui.h"
#include <cv.h>
int main( int argc, char** argv ) {
    IplImage* img = cvLoadImage( argv[1] );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage( "Example1", img );
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow( "Example1" );
}

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH ${/home/jinder1s/Documents/project/opencv/FindOpenCV.make} )
project(hello)
Find_package (OpenCV REQUIRED)
if(OpenCV_FOUND)
    add_executable (Hello test.cpp)
    find_library(Opencv_lib 
    NAMES opencv_core opencv_highgui opencv_imgproc
    PATHS /usr/local/lib)
endif()

this is the template for what I got.

jinder1s@jinder1s-lat-lap:~/Documents/project/opencv/tests$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jinder1s/Documents/project/opencv/tests
jinder1s@jinder1s-lat-lap:~/Documents/project/opencv/tests$ make
Linking CXX executable Hello
CMakeFiles/Hello.dir/test.cpp.o: In function `main':
test.cpp:(.text+0x1d): undefined reference to `cvLoadImage'
test.cpp:(.text+0x35): undefined reference to `cvNamedWindow'
test.cpp:(.text+0x49): undefined reference to `cvShowImage'
test.cpp:(.text+0x55): undefined reference to `cvWaitKey'
test.cpp:(.text+0x61): undefined reference to `cvReleaseImage'
test.cpp:(.text+0x6d): undefined reference to `cvDestroyWindow'
collect2: error: ld returned 1 exit status
make[2]: *** [Hello] Error 1
make[1]: *** [CMakeFiles/Hello.dir/all] Error 2
make: *** [all] Error 2

I just started learning opencv, as in this is my first code, and I just can't seem to get it to work. I could really use some help here. Can't see what I'm doing wrong.

0

2 Answers 2

3

You have to tell cmake to link with the library after checking that the library is available.

Something like target_link_libraries( Hello ${OpenCV_LIBS} ) just before your endif()

See http://www.cmake.org/cmake/help/cmake_tutorial.html for how to use CMake, and http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html for CMake with openCV.

You can check how your linker is called by typing "make VERBOSE=1". It helps debugging cmake files.

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

2 Comments

To find the libraries, you use the CMake module located at "/home/jinder1s/Documents/project/opencv/FindOpenCV.make" (that's what says the first line). Inside this file, you may find the name of the variable where the OpenCV LIBS you asked for (with find_library) are stored. Maybe it's note exactly "OpenCV_LIBS". You can check what's inside the ${OpenCV_LIBS} variable, you can write message(${OpenCV_LIBS}) in your CMakeLists.
Also, your set(CMAKE_MODULE_PATH path) is strange. For an absolute path, it should be set(CMAKE_MODULE_PATH /home/jinder1s/Documents/project/opencv/FindOpenCV.make). You would like to use something more portable like set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") and copy the FindOpenCV.cmake in yourproject/cmake/Modules/ directory. But since cmake do not complains, your issue isn't related to this point.
0

I think you need to specify the namespace there. So either do a using namespace cv; up top, or cv::function_name for every function call.

Also, this looks like OpenCV 1.x code. Is there a reason why you're doing that instead of using OpenCV 2.x syntax? 2.x is way more stable and intuitive. For example, that load image line would just be Mat img = imread(filename); in 2.x. And you'd need #include "opencv2/core/core.hpp" and using namespace cv; up top, for 2.x.

This cheatsheet may help.

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.