darel
darel

Reputation: 128

Opencv 'undefined reference to `cv::namedWindow....' (linking error)

I'm using the following CMake file to build an opencv project using the command cmake followed by make.

cmake_minimum_required(VERSION 2.8)
project(t)
#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/keiths/opencv/opencv-2.4.11_build/build)
SET(CMAKE_C_COMPILER mpicc)
SET(CMAKE_CXX_COMPILER mpicxx)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(${OpenCV_INCLUDE_DIRS})

find_package(OpenCV REQUIRED)
find_package(MPI REQUIRED)

add_executable(t t.cpp)
target_link_libraries(t ${OpenCV_LIBS} ${OpenCV_LIBRARIES} opencv_core opencv_highgui opencv_calib3d opencv_contrib opencv_core opencv_features2d opencv_flann opencv_gpu opencv_highgui opencv_imgproc opencv_legacy opencv_ml opencv_nonfree opencv_objdetect opencv_ocl opencv_photo opencv_stitching opencv_superres opencv_ts opencv_video opencv_videostab rt pthread m dl) 

MESSAGE(${OpenCV_LIBS})
MESSAGE(${OpenCV_INCLUDE_DIRS})

cmake completes fine, but make gives me the following error:

CMakeFiles/t.dir/t.cpp.o: In function main': t.cpp:(.text+0x56): undefined reference to cv::namedWindow(std::__cxx11::basic_string, std::allocator > const&, int)' collect2: error: ld returned 1 exit status make[2]: * [t] Error 1 make[1]: * [CMakeFiles/t.dir/all] Error 2 make: *** [all] Error 2

I've tried running the following command

g++ t.cpp $(pkg-config --libs opencv --cflags)

but get the following error

/home/keiths/tmp/ccWFcaXH.o: In function main': t.cpp:(.text+0x56): undefined reference to cv::namedWindow(std::__cxx11::basic_string, std::allocator > const&, int)' collect2: error: ld returned 1 exit status

I'm running the following simple code

#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include <mpi.h>

 using namespace cv;

 int main(int argc, char **argv) {    
      Mat image;
      namedWindow( "Display window", WINDOW_AUTOSIZE );
      return 0;
  }

I've tried as many recommendations as I can without success. Platform is CentOS and I'm a limited user (non-root) with my locally installed (more recent) versions of g++/gcc. I get the correct list of libraries for the pkg-config command and have configured library paths as well as the PATH to my local opencv folder

Just to add a very important point, I tried compiling the same code while explicitly specifying the stock g++ compiler (/usr/bin/g++) and all went well. So it seems that the problem would be with my local g++ compiler at '/home/keiths/lbin'. I need this latest version however for the c++11 capability (the old is 4.4.7 and it would take ages for the Sysadmin to upgrade it)

Upvotes: 2

Views: 2158

Answers (1)

darel
darel

Reputation: 128

I had built my OpenCV library using the old g++ compiler (in the system bin folder), prior to building the latest g++. Rebuilding the OpenCV library using the new g++ compiler did it for me. Out of curiosity, I tried building the OpenCV example using the old compiler and it returned many 'undefined reference' lines, but I'm happy to swap problems since I don't need it anyway!

Upvotes: 3

Related Questions