【问题标题】:Simple Qt project with openCV instantly crash带有openCV的简单Qt项目立即崩溃
【发布时间】:2016-08-10 08:18:26
【问题描述】:

我尝试在 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 上工作。

有人看到我可能犯的错误吗?

【问题讨论】:

  • 你能在调试器下运行这个程序吗?
  • @tty6 我写道,当我这样做时(在 Qt Creator 下按 f5),我只会得到“CDB 进程终止”。所以我没有得到任何真实的信息。

标签: c++ qt opencv


【解决方案1】:

我的设置与您的设置相似,并且遇到了完全相同的问题。问题是未定义相应 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】:

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

    【讨论】:

    • add Windows Path => Dll文件夹为我解决了这个问题,我按照教程用QT构建opencv(我的QT之前打开过,这会导致问题),成功构建opencv后,我运行没有退出的例子已经打开QT,程序崩溃了,因为我在构建opencv期间设置了DLL路径,所以我需要关闭QT重新打开QT,让它识别opencv构建的dll位置。
    【解决方案3】:

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

    1. 您正在使用的 Qt 安装。
    2. OpenCV。
    3. 您的代码。

    很可能上述至少一项不是使用相同的编译器构建的。如果 OpenCV 与 Qt 相关联,那么它也必须是针对二进制兼容版本的 Qt 构建的。

    【讨论】:

    • Qt 是特定于 msvc 版本的,因此项目工具包使用相同的编译器,我使用该特定编译器编译 openCV(全部为 msvc2012)。
    • 这是不正确的,除非您正在为 Qt Creator 创建插件。用于运行代码的 OpenCV 构建和编译器应该相同。
    • Qt Creator 是从哪里提出这个问题的? IDE 不相关。 Qt、OpenCV 和您的代码必须使用相同的编译器构建,或者使用供应商声明的编译器以具有二进制兼容输出。对于 MSVC,这意味着相同的调试/非调试运行时库选择,以及从 MSVC 2017 开始的编译器,因为 MSVC 2019 在很大程度上与 2017 二进制兼容。但有时并非如此。所以这是个问题。当然,MINGW Qt 构建不适用于 MSVC 构建的用户代码。构建 Qt 是一项相当容易的任务,所以我不太明白担心在哪里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多