Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Subdirs project with QML LNK2001Error

    General and Desktop
    3
    4
    238
    Loading More Posts
    • 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
      robro last edited by

      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-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @robro last edited by raven-worx

        @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 Reply Quote 4
        • R
          robro last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 2
            • First post
              Last post