undefined reference to `cv::imread

asked 2016-11-18 13:50:38 -0500

cagdas001 gravatar image

updated 2016-11-18 14:13:47 -0500

berak gravatar image

Hi, I'm using CLion as IDE and trying to link OpenCV. But I'm having problem even with a very simple app.

I prepared OpenCV binaries with these steps.

  1. Extract OpenCV files (from official website)

  2. Generate a build folder with CMake

  3. In the directory generated by CMake, execute "mingw32-make" command

  4. Add "OPENCV_DIR" to system variables. ("C:\OpenCV\mingw-build" in my case)

  5. Add "%OPENCV_DIR%\bin" to system path

My CPP Code:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;

int main() {
string file = "image.jpg";
Mat img = imread(file.c_str(), 1);
namedWindow("image", WINDOW_AUTOSIZE);
imshow("image", img);
return 0;

My CMakeLists.txt

cmake_minimum_required(VERSION 3.6)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

find_package(OpenCV REQUIRED)

set(SOURCE_FILES main.cpp)
add_executable(OpenCVFirstDemo ${SOURCE_FILES})

target_link_libraries(OpenCVFirstDemo ${OpenCV_LIBS})

And when I try to build, I'm getting the following messages.

"C:\Program Files (x86)\JetBrains\CLion 2016.2.3\bin\cmake\bin\cmake.exe" --build C:\Users\CagdasX\.CLion2016.2\system\cmake\generated\OpenCVFirstDemo-f9ecc709\f9ecc709\Debug --target OpenCVFirstDemo -- -j 8
Scanning dependencies of target OpenCVFirstDemo
[ 50%] Building CXX object CMakeFiles/OpenCVFirstDemo.dir/main.cpp.obj
[100%] Linking CXX executable OpenCVFirstDemo.exe
CMakeFiles\OpenCVFirstDemo.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/CagdasX/ClionProjects/OpenCVFirstDemo/main.cpp:9: undefined reference to `cv::imread(cv::String const&, int)'
C:/Users/CagdasX/ClionProjects/OpenCVFirstDemo/main.cpp:10: undefined reference to `cv::namedWindow(cv::String const&, int)'
C:/Users/CagdasX/ClionProjects/OpenCVFirstDemo/main.cpp:11: undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:/Users/CagdasX/ClionProjects/OpenCVFirstDemo/main.cpp:12: undefined reference to `cv::waitKey(int)'
CMakeFiles\OpenCVFirstDemo.dir/objects.a(main.cpp.obj): In function `ZN2cv6StringC1EPKc':
C:/OpenCV/sources/modules/core/include/opencv2/core/cvstd.hpp:625: undefined reference to `cv::String::allocate(unsigned int)'
CMakeFiles\OpenCVFirstDemo.dir/objects.a(main.cpp.obj): In function `ZN2cv6StringD1Ev':
C:/OpenCV/sources/modules/core/include/opencv2/core/cvstd.hpp:667: undefined reference to `cv::String::deallocate()'
CMakeFiles\OpenCVFirstDemo.dir/objects.a(main.cpp.obj): In function `ZN2cv3MatD1Ev':
C:/OpenCV/sources/modules/core/include/opencv2/core/mat.inl.hpp:571: undefined reference to `cv::fastFree(void*)'
CMakeFiles\OpenCVFirstDemo.dir/objects.a(main.cpp.obj): In function `ZN2cv3Mat7releaseEv':
C:/OpenCV/sources/modules/core/include/opencv2/core/mat.inl.hpp:682: undefined reference to `cv::Mat::deallocate()'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\OpenCVFirstDemo.dir\build.make:112: recipe for target 'OpenCVFirstDemo.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/OpenCVFirstDemo.dir/all' failed
mingw32-make.exe[3]: *** [OpenCVFirstDemo.exe] Error 1
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/OpenCVFirstDemo.dir/rule' failed
mingw32-make.exe[2]: *** [CMakeFiles/OpenCVFirstDemo.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/OpenCVFirstDemo.dir/rule] Error 2
Makefile:117: recipe for target 'OpenCVFirstDemo' failed
mingw32-make.exe: *** [OpenCVFirstDemo] Error 2
edit retag flag offensive close merge delete



would you do us a favour, and replace the screenshots with a text version ?

(so it can be indexed properly for search, ppl here can quote you correctly, and so-on)

berak gravatar imageberak ( 2016-11-18 13:55:56 -0500 )edit

the prebuild libs from SF do not cover mingw (you can't use VS libs with mingw, and g++ is notoriously bad at reporting such a problem)

get mingw-64 and cmake,, and build from (rather github) src, before trying to build your own project.

berak gravatar imageberak ( 2016-11-18 14:06:39 -0500 )edit

OK I replaced screenshots. I will try your response now then I would write the result

cagdas001 gravatar imagecagdas001 ( 2016-11-18 14:14:07 -0500 )edit

^^ yea, all cool. also, your recent edit puts me wrong about using VS libs. (we'll have to enquire further here)

berak gravatar imageberak ( 2016-11-18 14:17:35 -0500 )edit

Actually I gave the "sources" folder (extracted by OpenCV installer) to CMake-GUI. As far as I know sources folder is the same with github. And as build folder, I gave a empty folder ("mingw-build" in my case) After the "mingw32-make" command, generated DLLs and some exe files in "bin" folder. We already need these DLLs as far as I know, am I wrong? I added the "bin" folder to system path. But I can't compile the project still.

cagdas001 gravatar imagecagdas001 ( 2016-11-18 14:29:19 -0500 )edit

"bin" folder to system path it's only for execution not for link.

When you build your solution or makefile using your CMakeLists.txt have you delete CMakeCache.txt? If it find it may be it is the old folder.

If you build your own opencv in mingw-build folder CMake normally won't be able to find opencv and you will have to give this folder in cmake GUI have you?

LBerger gravatar imageLBerger ( 2016-11-18 15:31:26 -0500 )edit

Yes I tried it, already my IDE doing that with "Reset CMake Cache and Reload" option. I'm giving "sources" folder of OpenCV to CMake-GUI

cagdas001 gravatar imagecagdas001 ( 2016-11-19 05:08:13 -0500 )edit

No don't give source folder but "mingw-build"

LBerger gravatar imageLBerger ( 2016-11-19 05:13:06 -0500 )edit

Which folder should I give? There are 2 folders. (sources and build)

cagdas001 gravatar imagecagdas001 ( 2016-11-19 05:42:14 -0500 )edit

In cmake gui you give "where is source code" and "where to build binaries".

where is source code is source opencv folder (where you put your download or git clone)

"where to build binaries" is place where your makefile will do everything you mustfind in this folder CMakeCache.txt and OpenCVModules.cmake.

For your project you have to give "where to build binaries" for OpenCV_DIR (in cmakegui)

LBerger gravatar imageLBerger ( 2016-11-19 05:52:28 -0500 )edit