OPENCV error: undefined reference to “cvLoadImage”-Collection of common programming errors

I have two files:

test.cpp :

#include "highgui.h"
#include 
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.

  1. 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.

Originally posted 2013-11-06 03:11:15.