简体   繁体   English

Qt(OSX)和OpenCV错误

[英]Qt (OSX) and OpenCV errors

I've been struggling with making OpenCV work with Qt, but for some reason isn't it able to recognize OpenCV libraries. 我一直在努力使OpenCV与Qt一起使用,但是由于某种原因,它无法识别OpenCV库。 I've already tried nearly all the tutorials on stackoverflow, but for some reason it does not work. 我已经在stackoverflow上尝试了几乎所有教程,但是由于某种原因,它不起作用。

The error message i get is. 我得到的错误消息是。

10:50:36: Running steps for project test...
10:50:36: Configuration unchanged, skipping qmake step.
10:50:36: Starting: "/usr/bin/make" 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -headerpad_max_install_names -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -mmacosx-version-min=10.7 -Wl,-rpath,/Users/me/Qt/5.4/clang_64/lib -o test.app/Contents/MacOS/test main.o mainwindow.o moc_mainwindow.o   -F/Users/me/Qt/5.4/clang_64/lib -L/usr/local/lib -framework QtWidgets -framework QtGui -framework QtCore -framework DiskArbitration -framework IOKit -framework OpenGL -framework AGL 
Undefined symbols for architecture x86_64:
  "cv::_InputArray::_InputArray(cv::Mat const&)", referenced from:
      MainWindow::MainWindow(QWidget*) in mainwindow.o
  "cv::Mat::deallocate()", referenced from:
      cv::Mat::release() in mainwindow.o
  "cv::imread(std::string const&, int)", referenced from:
      MainWindow::MainWindow(QWidget*) in mainwindow.o
  "cv::imshow(std::string const&, cv::_InputArray const&)", referenced from:
      MainWindow::MainWindow(QWidget*) in mainwindow.o
  "cv::fastFree(void*)", referenced from:
      cv::Mat::~Mat() in mainwindow.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test.app/Contents/MacOS/test] Error 1
10:50:37: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project test (kit: Desktop Qt 5.4.0 clang 64bit)
When executing step "Make"
10:50:37: Elapsed time: 00:00.

this is my .pro file 这是我的.pro文件

#-------------------------------------------------
#
# Project created by QtCreator 2015-01-23T19:58:31
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test
TEMPLATE = app

INCLUDEPATH += /usr/local/include

HEADERS  += mainwindow.h

LIBS += -L/usr/local/lib
        -lopencv_core
        -lopencv_imgcodecs
        -lopencv_highgui
        -lopencv_imgproc

SOURCES += main.cpp\
        mainwindow.cpp


FORMS    += mainwindow.ui

mainwindow.h mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

main.cpp main.cpp中

#include "mainwindow.h"
#include <QApplication>
#include <iostream>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    std::cout << "hello world - lalala" << std::endl;

    return a.exec();
}

mainwindow.cpp mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    cv::Mat inputImage = cv::imread("/users/horse.jpg");
    cv::imshow("test",inputImage);
}

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

mainwindow.ui mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>797</width>
    <height>525</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>350</x>
      <y>330</y>
      <width>113</width>
      <height>32</height>
     </rect>
    </property>
    <property name="text">
     <string>done</string>
    </property>
   </widget>
   <widget class="QGraphicsView" name="input">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>70</y>
      <width>256</width>
      <height>192</height>
     </rect>
    </property>
   </widget>
   <widget class="QGraphicsView" name="output">
    <property name="geometry">
     <rect>
      <x>490</x>
      <y>70</y>
      <width>256</width>
      <height>192</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>797</width>
     <height>22</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuOpenCV">
    <property name="title">
     <string>OpenCV</string>
    </property>
   </widget>
   <addaction name="menuOpenCV"/>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

I found the solution to my problem 我找到了解决我问题的方法

https://qt-project.org/forums/viewthread/35646 https://qt-project.org/forums/viewthread/35646

I solve this by change 我通过改变来解决

/Users/ObiWang/Qt5.2.0/5.2.0-rc1/clang_64/mkspecs/macx-clang/qmake.conf /Users/ObiWang/Qt5.2.0/5.2.0-rc1/clang_64/mkspecs/macx-clang/qmake.conf

from QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.6 to 从QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.6到

QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9 Then there is no error message. QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9然后没有错误消息。

only 10.9 works ( 10.6, 10.7, and 10.8 all failed ) 仅10.9有效(10.6、10.7和10.8均失败)

I see two possible problems: 我看到两个可能的问题:

In the .pro file: 在.pro文件中:

LIBS += -L/usr/local/lib

--> Are openCV libs within this directory? ->这个目录中是否包含openCV库? (Otherwise try to add the directory of openCV libs with another -L flag) (否则,尝试使用另一个-L标志添加openCV库的目录)

From the compilation error: 从编译错误:

ld: symbol(s) not found for architecture x86_64

Could it be, that you have the 32-bit versions of the library, instead of the 64-bit version? 难道您拥有该库的32位版本,而不是64位版本? (You can check using the file command: eg file opencv_core.so .) (您可以使用file命令进行检查:例如file opencv_core.so。)

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

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