Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Subdirs project with QML LNK2001Error

Subdirs project with QML LNK2001Error

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 472 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robro
    wrote on last edited by
    #1

    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 GUI.pro 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
    

    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "qimageviewer.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        qmlRegisterType<QImageViewer>("com.bri.classes", 1, 0, "ImageViewer");
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    qimageviewer.h:

    #ifndef QIMAGEVIEWER_H
    #define QIMAGEVIEWER_H
    
    #include <QQuickItem>
    #include <QQuickPaintedItem>
    #include <QImage>
    #include <QPainter>
    
    class QImageViewer : public QQuickPaintedItem
    {
        Q_OBJECT
    public:
        QImageViewer(QQuickItem *parent = Q_NULLPTR);
        Q_INVOKABLE void setImage(const QImage &img);
    private:
        QImage currentImage;
        void paint(QPainter *painter);
    };
    
    #endif // QIMAGEVIEWER_H
    
    raven-worxR 1 Reply Last reply
    0
    • R robro

      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 GUI.pro 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
      

      main.cpp:

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include "qimageviewer.h"
      
      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
          QGuiApplication app(argc, argv);
      
          qmlRegisterType<QImageViewer>("com.bri.classes", 1, 0, "ImageViewer");
      
          QQmlApplicationEngine engine;
          engine.load(QUrl(QLatin1String("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
      
          return app.exec();
      }
      

      qimageviewer.h:

      #ifndef QIMAGEVIEWER_H
      #define QIMAGEVIEWER_H
      
      #include <QQuickItem>
      #include <QQuickPaintedItem>
      #include <QImage>
      #include <QPainter>
      
      class QImageViewer : public QQuickPaintedItem
      {
          Q_OBJECT
      public:
          QImageViewer(QQuickItem *parent = Q_NULLPTR);
          Q_INVOKABLE void setImage(const QImage &img);
      private:
          QImage currentImage;
          void paint(QPainter *painter);
      };
      
      #endif // QIMAGEVIEWER_H
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      4
      • R Offline
        R Offline
        robro
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved