Important: Please read the Qt Code of Conduct -

Usage of abstract class in a dll

  • 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
    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
    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"

    : 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 toMasterModel::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.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

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

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


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


    QT -= gui

    TARGET = Model
    TEMPLATE = lib


    SOURCES += model.cpp

    HEADERS += model.h

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

    LIBS += -fPIC

  • Lifetime Qt Champion

    You are not compiling master model.cpp

  • 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?

  • Lifetime Qt Champion

    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.

Log in to reply