Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Usage of abstract class in a dll
Forum Updated to NodeBB v4.3 + New Features

Usage of abstract class in a dll

Scheduled Pinned Locked Moved C++ Gurus
7 Posts 2 Posters 3.6k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    kwurpi
    wrote on last edited by
    #1

    Folks,
    I wonder if this is the right place to ask my question - because a guru would certainly not have to ask at all.

    I'd like to write a .dll which uses an abstract class defined in the project.

    Say this class would be:
    @
    #ifndef MASTERMODEL_H
    #define MASTERMODEL_H

    class MasterModel
    {
    public:
    MasterModel();
    virtual double getY(int t) = 0;
    };

    #endif // MASTERMODEL_H
    @

    the header for the dll would use this abstract class:
    @
    #ifndef MODEL_H
    #define MODEL_H

    #include "model_global.h"
    #include "mastermodel.h"

    class MODELSHARED_EXPORT Model : public MasterModel
    {
    public:
    Model();
    virtual double getY(int t);
    };

    #endif // MODEL_H
    @
    "model_global.h" is defining MODELSHARED_EXPORT to be Q_DECL_EXPORT
    and the .cpp
    @
    #include "model.h"

    Model::Model()
    : MasterModel()
    {}

    double Model::getY(int t) {
    return t/2.1;
    }

    extern "C" __declspec(dllexport) MasterModel *getModel()
    {
    return new Model();
    }
    @

    I'd use the .dll in the in the project with "LoadLibrary" and GetProcAddress(libHandle, "getModel")
    but this is not the problem.

    The problem occurs when building the .dll. I would get:

    ...\Model\model.cpp:4: error: undefined reference to `MasterModel::MasterModel()'

    Anyone out there who could tell me how to get it right?
    Thanks a lot for your help.

    @
    20:29:26: Running steps for project Model...
    20:29:26: Configuration unchanged, skipping qmake step.
    20:29:26: Starting: "C:\Qt\Tools\mingw48_32\bin\mingw32-make.exe"
    C:/Qt/Tools/mingw48_32/bin/mingw32-make -f Makefile.Debug
    mingw32-make[1]: Entering directory 'C:/Users/kraus/Desktop/HK/build-Model-Debug'
    g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DMODEL_LIBRARY -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_CORE_LIB -I..\Model -I"..\Geom5" -I"..........\Qt\5.2.1\mingw48_32\include" -I"..........\Qt\5.2.1\mingw48_32\include\QtCore" -I"debug" -I"." -I"..........\Qt\5.2.1\mingw48_32\mkspecs\win32-g++" -o debug\model.o ..\Model\model.cpp
    g++ -shared -mthreads -Wl,--out-implib,debug\libModel.a -o debug\Model.dll debug/model.o -fPIC -LC:\Qt\5.2.1\mingw48_32\lib -lQt5Cored
    debug/model.o: In function ZN5ModelC2Ev': C:\Users\kraus\Desktop\HK\build-Model-Debug/../Model/model.cpp:4: undefined reference to MasterModel::MasterModel()'
    collect2.exe: error: ld returned 1 exit status
    Makefile.Debug:77: recipe for target 'debug\Model.dll' failed
    mingw32-make[1]: *** [debug\Model.dll] Error 1
    mingw32-make[1]: Leaving directory 'C:/Users/kraus/Desktop/HK/build-Model-Debug'
    mingw32-make: *** [debug] Error 2
    makefile:34: recipe for target 'debug' failed
    20:29:29: The process "C:\Qt\Tools\mingw48_32\bin\mingw32-make.exe" exited with code 2.
    Error while building/deploying project Model (kit: Desktop Qt 5.2.1 MinGW 32bit)
    When executing step 'Make'
    20:29:29: Elapsed time: 00:03.
    @

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

      Hi and welcome to devnet,

      You are missing the implementation of the constructor of MasterModel. Just add that and your build shale succeed

      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
      0
      • K Offline
        K Offline
        kwurpi
        wrote on last edited by
        #3

        I forgot to poste, but I have the .cpp - even though it's still empty:
        @
        #include "mastermodel.h"

        MasterModel::MasterModel()
        {
        }
        @

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kwurpi
          wrote on last edited by
          #4

          It must have to do with the .pro file but can't find out what.

          @

          QT -= gui

          TARGET = Model
          TEMPLATE = lib

          DEFINES += MODEL_LIBRARY

          SOURCES += model.cpp

          HEADERS += model.h
          model_global.h
          ../Geom5/mastermodel.h

          unix {
          target.path = /usr/lib
          INSTALLS += target
          }
          INCLUDEPATH +=../Geom5

          LIBS += -fPIC
          @

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

            You are not compiling master model.cpp

            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
            0
            • K Offline
              K Offline
              kwurpi
              wrote on last edited by
              #6

              Yes, you are right.
              If I add mastermodel.cpp to the SOURCES list then things are fine.
              But I thought the whole point is to avoid the need to compile the abstract class.

              I mean in my real project the abstract class (mastermodel) includes a method which is the same for all derived classes in the dll, but they don't have to re-implement this method themselves.

              Am I trying to do nonsense?

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

                An abstract class doesn't mean no implementation at all. It only means that you can't instantiate it because you have one or more pure virtual methods. Take a look at the various abstract classes from Qt, most of them if not all have one or more method already implemented.

                An abstract interface is different, in that case you only have pure virtual functions so no contractor etc. You can take a look at e.g. the plugin chapter in Qt's documentation.

                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
                0

                • Login

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