opencv 3.1 Undefined Reference

asked 2016-06-28 12:48:21 -0600

lizo gravatar image

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

Here is my DisplayImage.cpp:

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main(int argc, char** argv )

    Mat image;
    image = imread( argv[1], 1 );

    imshow("Display Image", image);


    return 0;

*Extra headers because I wanted to make sure I wasn't forgetting an include

cmake . works After running make:

[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[100%] Linking CXX executable DisplayImage
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `main':
DisplayImage.cpp:(.text+0x5f): undefined reference to `cv::imread(cv::String const&, int)'
DisplayImage.cpp:(.text+0xd0): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `cv::String::String(char const*)':
DisplayImage.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4f): undefined reference to `cv::String::allocate(unsigned long)'
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `cv::String::~String()':
DisplayImage.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
make[2]: *** [DisplayImage] Error 1
make[1]: *** [CMakeFiles/DisplayImage.dir/all] Error 2
make: *** [all] Error 2
edit retag flag offensive close merge delete


What version of opencv? Why are you including imgproc twice?

Does this not work (for opencv3):

#include "opencv2/core.hpp"
#include "opencv2/face.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect.hpp"
atv gravatar imageatv ( 2016-06-28 14:52:49 -0600 )edit

Can you replace ${OpenCV_LIBS} with opencv_core, opencv_highgui, and opencv_imgcodecs, and see if it fixes the linking? If yes, then, your variable is not set.

unxnut gravatar imageunxnut ( 2016-06-28 17:13:42 -0600 )edit