使用openCV的简单Qt项目立即崩溃

时间:2016-08-10 08:18:26

标签: c++ qt opencv

我尝试在Qt项目中使用openCV。但是如果我链接openCV的发布库,我的发布版本会在启动时立即崩溃。调试库允许程序启动但是当我尝试使用openCV函数时应用程序崩溃(已知在openCV中混合发布/调试会导致一些崩溃)。

所以我做了一个简单的项目,它甚至都没有启动。释放和调试构建崩溃并使用调试器会导致一个小窗口显示“意外的CDB退出”。

以下是来源。

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test_openCV
TEMPLATE = app

#flags to generate a .map file
QMAKE_LFLAGS_RELEASE +=/MAP
QMAKE_LFLAGS_RELEASE += /debug

SOURCES += main.cpp\
        MainWindow.cpp

HEADERS  += MainWindow.h

FORMS    += MainWindow.ui

INCLUDEPATH += $$PWD
INCLUDEPATH += "D:/openCV/build/include"

#Switching between handbuild and the build I downloaded have no effect.
#I am sure the path are good. Quadra checked.
#LIBS += -L"D:/openCV/build/x64/vc11/lib"
LIBS += -L"D:/openCV/hand_build/lib/Release"
LIBS += -L"D:/openCV/hand_build/lib/Debug"

#disables the "fopen not secure" warning in openCV.    
DEFINES += _CRT_SECURE_NO_WARNINGS

win32:CONFIG(release, debug|release): LIBS += -lopencv_core2413 -lopencv_highgui2413 -lopencv_imgproc2413
else:win32:CONFIG(debug, debug|release): LIBS += -lopencv_core2413d -lopencv_highgui2413d -lopencv_imgproc2413d

main.cpp中:

#include "MainWindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv/cv.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

和MainWindow.cpp:

#include "MainWindow.h"
#include "ui_MainWindow.h"

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //Removing this line will causes the program to start normally (guess it won't link the libs if nothing from openCV is used).
    cv::Mat image;
}

MainWindow::~MainWindow()
{
    delete ui;
}

启动应用时我得到了什么:

Starting D:\Colin\build_test_openCV\release\test_openCV.exe...
program suddenly ended
D:\Colin\build_test_openCV\release\test_openCV.exe crashed

我在Windows 7 / MSCV2012 openGL 64bits / Qt 5.2.1 openGL上工作。

有没有人看到我本可以犯的错误?

3 个答案:

答案 0 :(得分:1)

构建中的所有项目必须使用相同的Visual Studio版本构建,这意味着:

  1. 您正在使用的Qt安装。
  2. 的OpenCV。
  3. 您的代码。
  4. 很可能至少有一个上面的内容不是使用相同的编译器构建的。如果OpenCV与Qt链接,它必须是针对Qt的二进制兼容版本构建的。

答案 1 :(得分:0)

我有与您相似的设置,并且遇到了完全相同的问题。问题是没有定义相应dll的路径。这些dll:

  • opencv_core2411.dll
  • opencv_highgui2411.dll
  • opencv_imgproc2411.dll

应该在D:/ openCV / hand_build / bin /(或者可能是D:/ openCV / hand_build / bin / Release /)。添加另一行:

LIBS += -L"D:/openCV/hand_build/bin/Release/"

应该有用。

答案 2 :(得分:0)

我刚才有类似的问题。该程序崩溃,因为它无法在运行时找到合适的DLL。将OpenCV目录添加到我的Windows PATH为我解决了这个问题。