Dzung Nguyen
Dzung Nguyen

Reputation: 3944

Undefined reference when using Cmake and QtCreator

I'm running the basic OpenCV example with OpenCV3.0.0 dev:

project(ImageDenoise)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)

find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

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

Source code:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(int argc, char* argv[] )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], IMREAD_COLOR );

    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);

    waitKey(0);

    return 0;
}

When I import this project into QtCreator, I got the following linking errors when building project:

[100%] Building CXX object CMakeFiles/ImageDenoise.dir/main.cpp.o
Linking CXX executable ImageDenoise
CMakeFiles/ImageDenoise.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x7c): undefined reference to `cv::imread(cv::String const&, int)'
main.cpp:(.text+0xf5): undefined reference to `cv::namedWindow(cv::String const&, int)'
main.cpp:(.text+0x144): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'

However when I run cmake from command line and using make, then it works perfectly. What is the reason behind this?

dzung@Cronus:~/kSVD/build$ make
Scanning dependencies of target ImageDenoise
[100%] Building CXX object CMakeFiles/ImageDenoise.dir/main.cpp.o
Linking CXX executable ImageDenoise
[100%] Built target ImageDenoise
dzung@Cronus:~/kSVD/build$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  ImageDenoise  Makefile
dzung@Cronus:~/kSVD/build$ ./ImageDenoise 
usage: DisplayImage.out <Image_Path>

Upvotes: 1

Views: 1711

Answers (1)

Osteri
Osteri

Reputation: 65

I had exactly the same issue. I think this is some sort of a Qt-creator bug.

I solved this by:

  1. Remove everything except *.cpp and CMakeLists.txt in the project folder.
  2. Before creating anything with Qt-creator do: cmake . && make
  3. Now open existing project in Qt-creator.
  4. Now you can run cmake/compile/run etc just fine.

Upvotes: 1

Related Questions