使用Cmake和QtCreator时未定义的引用

时间:2014-02-27 01:28:50

标签: c++ cmake qt-creator

我正在使用OpenCV3.0.0开发运行基本的OpenCV示例:

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})

源代码:

#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;
}

当我将此项目导入QtCreator时,我在构建项目时遇到以下链接错误:

[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&)'

但是当我从命令行运行cmake并使用make时,它运行得很好。这背后的原因是什么?

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>

1 个答案:

答案 0 :(得分:1)

我有完全相同的问题。我认为这是某种Qt创建者的错误。

我通过以下方式解决了这个问题:

  1. 删除项目中除* .cpp和CMakeLists.txt之外的所有内容 文件夹中。
  2. 在使用Qt-creator创建任何内容之前,请执行:cmake . && make
  3. 现在在Qt-creator中打开现有项目。
  4. 现在你可以运行cmake / compile / run等了。