Important: Please read the Qt Code of Conduct -

Subdirs project with QML LNK2001Error

  • Hello,

    I created a subdirs project in which I would like to separate between my C++ code (lib project) and my GUI (QML application).

    In my I include the path to the headers of my lib project and I also link the library.

    The class "QImageViewer" is part of the lib project.

    The problem is that "qmlRegisterType" in my main.cpp leads to a lot of LNK2001 errors: "Unresolved external symbol... file not found main.obj"
    One original error: "main.obj:-1: Fehler: LNK2001: Nicht aufgelöstes externes Symbol ""public: virtual struct QMetaObject const * __cdecl QImageViewer::metaObject(void)const " (?metaObject@QImageViewer@@UEBAPEBUQMetaObject@@XZ)"."

    The main.obj is there and the lib builds fine. Just the GUI throws this error. I also cleaned everything several times and was running qmake again, but nothing helped.

    Do you have an idea, where the error is?
    Thank you very much!

    QT += qml quick
    QT += gui #QImage
    LIBS += -L$$PWD/../../build-SpecTool-Desktop_Qt_5_9_1_MSVC2017_64bit-Debug/SpecTool_lib/debug/ -lSpecTool_lib
    INCLUDEPATH += $$PWD/../SpecTool_lib
    DEPENDPATH += $$PWD/../SpecTool_lib


    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "qimageviewer.h"
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        qmlRegisterType<QImageViewer>("com.bri.classes", 1, 0, "ImageViewer");
        QQmlApplicationEngine engine;
        if (engine.rootObjects().isEmpty())
            return -1;
        return app.exec();


    #ifndef QIMAGEVIEWER_H
    #define QIMAGEVIEWER_H
    #include <QQuickItem>
    #include <QQuickPaintedItem>
    #include <QImage>
    #include <QPainter>
    class QImageViewer : public QQuickPaintedItem
        QImageViewer(QQuickItem *parent = Q_NULLPTR);
        Q_INVOKABLE void setImage(const QImage &img);
        QImage currentImage;
        void paint(QPainter *painter);
    #endif // QIMAGEVIEWER_H

  • Moderators

    when using a class from another library in a different project the class needs to be properly exported.
    See this

  • Thank you very much!
    Using the export marco "class SPECTOOL_LIBSHARED_EXPORT QImageViewer" from ..._global.h created by Qt solved everything :-)

  • Lifetime Qt Champion


    Unrelated to your topic, but I'd recommend not prefixing your custom classes with Q, this will generate confusion for people reading your code (you included) and might clash one day with Qt if they implement a class named like that.

Log in to reply