【问题标题】:Linker error in OpenCV Program in Qt-CreatorQt-Creator 中 OpenCV 程序中的链接器错误
【发布时间】:2015-11-19 03:55:14
【问题描述】:

我在安装 OpenCV-3 的 Qt-Creator、qt-5.4 中收到以下错误:-

:-1: 错误:LNK1104: 无法打开文件'opencv_ts300.lib'

在我的项目的 .pro 文件中,我添加了 -

INCLUDEPATH += "E:\opencv\build\include"

LIBS += -L"E:\opencv\build\x86\vc11\lib"
LIBS += -lopencv_ts300d \ -lopencv_world300d \ -lopencv_ts300 \   -lopencv_world300d

以下是我的 main.cpp 程序:-

#include "mainwindow.h"
#include <QApplication>
#include<opencv2/opencv.hpp>

using namespace cv; // else would have to specify cv::cvtColor() etc.


int main(int argc, char *argv[])
{

QApplication a(argc, argv);
MainWindow w;
w.show();
char * imageName ="2-dp.JPG" ; // read the name of image to be loaded called   3-dp.JPG

Mat image;                  // declare Mat type object to load image-matrix

image = imread(imageName,1); // image object now contains image-matrix of the image loaded

if(!image.data)  // if image-data was invalid/non-existent/could not be read
{
    printf("No image data to load \n"); // then print messg
    return -1;                          // and exit program with exit code -1
}

Mat gray_image;            // Mat object declared for the image after gray-scale conversion

cvtColor(image, gray_image, CV_BGR2GRAY); // converts from BGR/RGB color space to gray scale

/*imwrite ("Gray_Image.jpg",gray_image);*/ //writes image-data (converted to gray scale iamge) into disk in JPEG format at file path given

namedWindow(imageName,CV_WINDOW_AUTOSIZE); // creates a named window for original image s.t window auto-scales its size as per image
namedWindow("Gray Image",CV_WINDOW_AUTOSIZE);

imshow(imageName,image);    // displays the image on the namedWindow by giving name of namedWindow
imshow("Gray Image",gray_image);

waitKey(0); //waits forever till user presses key


return a.exec();
}

请帮忙!

【问题讨论】:

    标签: c++ qt opencv


    【解决方案1】:

    联动

    反斜杠符号\ 不应在一行中的库名称之间使用。它没有给出问题中提到的错误消息,但是链接失败并显示相同的错误代码。

    反斜杠符号可用于分隔长行:

    LIBS += -lopencv_ts300d \
            -lopencv_world300d
    

    否则库应该在没有\的一行中列出:

    LIBS += -lopencv_ts300 -lopencv_world300
    

    运行时

    在运行时可能会发现一些问题。

    有两种库类型:调试(以后缀d 标记)和发布。它们不应该混合在一个版本中。使用错误的库类型可能会导致运行时崩溃,例如OpenCV in Qt crashes on deallocation of vector of lines when using HoughLinesP

    可以编写 qmake .pro 文件,根据发布或调试模式仅使用适当的库:

    INCLUDEPATH += "E:\opencv\build\include"
    
    LIBS += -L"E:\opencv\build\x86\vc11\lib"
    
    CONFIG(release, debug|release) {
        # release libraries
        LIBS += -lopencv_ts300 -lopencv_world300
    }
    
    CONFIG(debug, debug|release) {
        # debug libraries
        LIBS += -lopencv_ts300d \
                -lopencv_world300d
    }
    

    注意E:\opencv\build\x86\vc11\bin 中也有二进制动态加载的库。如果在运行时无法找到这些库,则应用程序应该崩溃。该路径可以添加到系统PATH 环境变量中,或者可以将所需的库复制到应用程序文件夹或系统PATH 中列出的其他位置。


    确保您正在使用编译器、链接器和 Qt 构建 vc11。对于 MSVC2013,应使用文件夹 vc12 (E:\opencv\build\x86\vc12\lib) 中的库。

    【讨论】:

      猜你喜欢
      • 2016-11-08
      • 2014-03-15
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多