【问题标题】:Undefined symbols for architecture x86_64 Using QT6 with Opencv in OSX mac在 OSX mac 中使用 QT6 和 Opencv 架构 x86_64 的未定义符号
【发布时间】:2021-09-08 20:07:41
【问题描述】:

我在 mac osx 上使用 qt6 和 OpenCv4 遇到了这个问题。 我检查了这些link1link2link3link4,但都没有解决问题。

这是我得到的日志错误:

Undefined symbols for architecture x86_64:
      "cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
          _main in main.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: *** [opencv_test_qmake.app/Contents/MacOS/opencv_test_qmake] Error 1
    15:19:36: The process "/usr/bin/make" exited with code 2.
    Error while building/deploying project opencv_test_qmake (kit: Desktop Qt 6.1.1 clang 64bit)
    When executing step "Make"

【问题讨论】:

    标签: c++ qt opencv


    【解决方案1】:

    这个问题主要发生在不包含库时。

    使用 qmake 时,你的 .pro 文件需要是这样的:

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++11
    
    SOURCES += \
        main.cpp \
        dialog.cpp
    
    HEADERS += \
        dialog.h
    
    FORMS += \
        dialog.ui
    
    
    #Added Lines for OpenCV
    INCLUDEPATH += /usr/local/include/opencv4
    LIBS += -L/usr/local/lib \
    LIBS += -lopencv_core -lopencv_imgproc -lopencv_features2d -lopencv_highgui -lopencv_calib3d -lopencv_imgcodecs -lopencv_videoio
    QMAKE_MACOSX_DEPLOYMENT_TARGET = 11.0
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    请注意,如果您使用 imread,则需要库 -lopencv_imgcodecs - 使用 videoCapture 时需要库 lopencv_videoio

    以下 main.cpp 可用于使用图像和 imread 检查设置:

    #include "dialog.h"
    #include <QApplication>
    
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
    
        cv::Mat image= cv::imread("/Users/apple/Desktop/dog.jpg");
    
        // create image window named "My Image"
        cv::namedWindow("My Image");
        // show the image on window
        cv::imshow("My Image", image);
        // wait key for 5000 ms
        cv::waitKey(5000);
    
        w.show();
        return a.exec();
    }
    

    此 main.cpp 文件可用于检查 Videocapture 和设置:

    #include "opencv2/core.hpp"
    #include "opencv2/imgproc.hpp"
    #include "opencv2/highgui.hpp"
    #include "opencv2/videoio.hpp"
    #include <iostream>
    
    using namespace cv;
    using namespace std;
    
    void drawText(Mat & image);
    
    int main()
    {
        cout << "Built with OpenCV " << CV_VERSION << endl;
        Mat image;
        VideoCapture capture;
        capture.open(0);
        if(capture.isOpened())
        {
            cout << "Capture is opened" << endl;
            for(;;)
            {
                capture >> image;
                if(image.empty())
                    break;
                drawText(image);
                imshow("Sample", image);
                if(waitKey(10) >= 0)
                    break;
            }
        }
        else
        {
            cout << "No capture" << endl;
            image = Mat::zeros(480, 640, CV_8UC1);
            drawText(image);
            imshow("Sample", image);
            waitKey(0);
        }
        return 0;
    }
    
    void drawText(Mat & image)
    {
        putText(image, "Hello OpenCV",
                Point(20, 50),
                FONT_HERSHEY_COMPLEX, 1, // font face and scale
                Scalar(255, 255, 255), // white
                1, LINE_AA); // line thickness and type
    }
    

    如果使用 Cmake 而不是 qmake,您可以使用相同的 main.cpp 文件。 您唯一需要做的就是将这些行添加到 CmakeList.txt 的底部 其中opencv_test_102 是项目文件夹的名称。

    #1 Specify OpenCV folder, and take care of dependencies and includes
    set(OpenCV_DIR /usr/local/include/opencv4)
    find_package(OpenCV REQUIRED)
    include_directories(${OpenCV_INCLUDE_DIRS})
    
    
    #2 Specify OpenCV library
    target_link_libraries(opencv_test_102 PRIVATE ${OpenCV_LIBS})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 2015-09-14
      • 2013-02-07
      • 1970-01-01
      • 2014-09-16
      • 2012-08-20
      • 1970-01-01
      相关资源
      最近更新 更多