简体   繁体   English

Qt-Creator中OpenCV程序中的链接器错误

[英]Linker error in OpenCV Program in Qt-Creator

I get the following error in Qt-Creator, qt-5.4 with OpenCV-3 installation :- 我在使用OpenCV-3安装的Qt-Creator,qt-5.4中收到以下错误:-

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

In my project's .pro file I have added - 在项目的.pro文件中,我添加了-

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

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

The following is my main.cpp program :- 以下是我的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();
}

Please help! 请帮忙!

Linkage 连锁

The backslash symbol \\ should not be used between library names in one line. 不应在一行中的库名称之间使用反斜杠符号\\ It does not give the error message mentioned in the question, however linkage fails with the same error code. 它没有给出问题中提到的错误消息,但是链接失败并显示相同的错误代码。

The backslash symbol may be used to break long lines: 反斜杠符号可用于分隔长行:

LIBS += -lopencv_ts300d \
        -lopencv_world300d

Otherwise the libraries should be listed in one line without \\ : 否则,这些库应在一行中不包含\\列出:

LIBS += -lopencv_ts300 -lopencv_world300

Runtime 运行

There are few issues that may be revealed at runtime. 在运行时可能发现的问题很少。

There are two library types: debug (marked by suffix d ) and release. 有两种库类型:调试(用后缀d标记)和发行版。 They should not be mixed in one build. 不应将它们混合在一起。 Using wrong library type may lead to runtime crash, for example OpenCV in Qt crashes on deallocation of vector of lines when using HoughLinesP 使用错误的库类型可能会导致运行时崩溃,例如,在使用HoughLinesP时,在重新分配行向量时Qt中的OpenCV崩溃

It is possible to write qmake .pro file that uses only proper libraries depending on release or debug mode: 可以根据发布或调试模式编写仅使用适当库的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
}

Note that there are also binary dynamic loaded libraries in E:\\opencv\\build\\x86\\vc11\\bin . 请注意, E:\\opencv\\build\\x86\\vc11\\bin中也有二进制动态加载的库。 If those libraries cannot be found at runtime the application should crash. 如果在运行时找不到这些库,则应用程序应该崩溃。 That path may be added to the system PATH environment variable or required libraries may be copied to application folder or to other place listed in system PATH . 该路径可以添加到系统PATH环境变量中,或者可以将所需的库复制到应用程序文件夹或系统PATH列出的其他位置。


Make sure that you are using compiler, linker and Qt build for vc11 . 确保您正在使用vc11编译器,链接器和Qt构建。 For MSVC2013 the libraries from the folder vc12 ( E:\\opencv\\build\\x86\\vc12\\lib ) should be used. 对于MSVC2013,应使用文件夹vc12E:\\opencv\\build\\x86\\vc12\\lib )中的E:\\opencv\\build\\x86\\vc12\\lib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM