[Solved] Undefined reference problem



  • Hi. I am making application that will use shared libraries. I have problem building the shared library. In main app (exe) i have this header and according cpp file.

    @
    #ifndef MENUNODE_H
    #define MENUNODE_H

    #include <QString>
    #include <QList>
    #include <QLabel>
    #include "ActionResult.h"

    namespace Core
    {
    namespace UI
    {
    class MenuNode : public QLabel
    {
    Q_OBJECT
    public:
    explicit MenuNode(QWidget* Parent = 0);
    virtual ~MenuNode();
    QString Name;
    QList<MenuNode*> Nodes;
    virtual ActionResult Action();
    };
    }
    }

    #endif // MENUNODE_H
    @

    In shared library i have class that will be exported in its own cpp and h files. There is also a class that is used by this shared class. Here is its definition.

    @
    #ifndef ROOTSPEECH_H
    #define ROOTSPEECH_H

    #include <QString>
    #include <QList>
    #include "NeuroCommunicator/Core/UI/MenuNode.h"

    namespace Core
    {
    namespace UI
    {
    class RootSpeech: public MenuNode
    {
    Q_OBJECT
    public:
    RootSpeech(QWidget* parent = 0);
    ~RootSpeech();
    QString p;
    };
    }
    }

    #endif // ROOTSPEECH_H
    @

    and cpp file

    @
    #include "RootSpeech.h"

    namespace Core
    {
    namespace UI
    {
    RootSpeech::RootSpeech(QWidget* parent):MenuNode(parent){p = "asdf";}
    RootSpeech::~RootSpeech(){}
    }
    }
    @

    Now the problem is:
    @
    debug/RootSpeech.o: In function `RootSpeech':

    D:!Projekat\Speech-build-desktop/../Speech/RootSpeech.cpp:8: undefined reference to `Core::UI::MenuNode::MenuNode(QWidget*)'

    D:!Projekat\Speech-build-desktop/../Speech/RootSpeech.cpp:11: undefined reference to `Core::UI::MenuNode::~MenuNode()'

    D:!Projekat\Speech-build-desktop/../Speech/RootSpeech.cpp:8: undefined reference to `Core::UI::MenuNode::MenuNode(QWidget*)'

    D:!Projekat\Speech-build-desktop/../Speech/RootSpeech.cpp:11: undefined reference to `Core::UI::MenuNode::~MenuNode()'

    debug/RootSpeech.o: In function `~RootSpeech':

    D:!Projekat\Speech-build-desktop/../Speech/RootSpeech.cpp:16: undefined reference to `Core::UI::MenuNode::~MenuNode()'

    D:!Projekat\Speech-build-desktop/../Speech/RootSpeech.cpp:16: undefined reference to `Core::UI::MenuNode::~MenuNode()'

    D:!Projekat\Speech-build-desktop/../Speech/RootSpeech.cpp:16: undefined reference to `Core::UI::MenuNode::~MenuNode()'

    D:!Projekat\Speech-build-desktop/../Speech/RootSpeech.cpp:16: undefined reference to `Core::UI::MenuNode::~MenuNode()'

    debug/RootSpeech.o:D:!Projekat\Speech-build-desktop/../Speech/RootSpeech.cpp:16: more undefined references to `Core::UI::MenuNode::~MenuNode()' follow

    debug/moc_RootSpeech.o:D:!Projekat\Speech-build-desktop/debug/moc_RootSpeech.cpp:59: undefined reference to `Core::UI::MenuNode::qt_metacast(char const*)'

    debug/moc_RootSpeech.o:D:!Projekat\Speech-build-desktop/debug/moc_RootSpeech.cpp:64: undefined reference to `Core::UI::MenuNode::qt_metacall(QMetaObject::Call, int, void**)'

    debug/moc_RootSpeech.o:moc_RootSpeech.cpp:(.rdata+0x0): undefined reference to `Core::UI::MenuNode::staticMetaObject'

    debug/moc_RootSpeech.o:moc_RootSpeech.cpp:(.rdata$_ZTVN4Core2UI10RootSpeechE[vtable for Core::UI::RootSpeech]+0xe8): undefined reference to `Core::UI::MenuNode::Action()'

    collect2: ld returned 1 exit status
    @

    What I have tried so far: Run qmake from build then rebuild, including app.exe as lib, Q_DECLARE_INTERFACE, adding menuNode.h to .pro file... (can't remember any more).

    System is Windows 7 x64.

    Any help?



  • Do you use @Q_DECL_EXPORT@ and @Q_DECL_IMPORT@ ?



  • I tried that too but with no success. Ill try again, just tell me where to put it. (I think I tried both on MenuNode class but it didnt work)

    EDIT: On Q_DECL_EXPORT on MenuNode i got same errors. On Q_DECL_IMPORT i got errors on MenuNode.cpp and moc files.



  • I tried adding CORESHARED_EXPORT to MenuNode and adding this file but ain't working (also added define to .pro file):
    @
    #ifndef CORE_GLOBAL_H
    #define CORE_GLOBAL_H

    #include <QtCore/qglobal.h>

    #if defined(CORE_LIBRARY)

    define CORESHARED_EXPORT Q_DECL_EXPORT

    #else

    define CORESHARED_EXPORT Q_DECL_IMPORT

    #endif

    #endif // CORE_GLOBAL_H
    @

    Same errors (after switching Q_DECL... still errors).



  • Could you show us your .pro file please.

    As far as I understood your text an source code you are linking a shared lib against an app.exe (i.e. you have a shared lib that uses code that is defined in an exe)?



  • Hi,
    it is not possible to have the base class in the main executable and a derived class in a shared library.
    If you want to have the derived class in the shared library (class RootSpeech: public MenuNode) you have to put the base class also in a shared library (the same or another lib you also reference in the there).
    An other thing is, if you use static libraries, but the you can put everything directly into the exe :-)

    So your problem is not a problem of exporting, it's more a problem of putting the right stuff in the right binary.



  • Here's my pro of shared library.
    @
    CONFIG += debug_and_release build_all
    CONFIG(release){ DESTDIR = ../build-release/plugins }
    CONFIG(debug){ DESTDIR = ../build-debug/plugins }

    QT += core gui xml
    TARGET = Speech
    TEMPLATE = lib
    INCLUDEPATH += ../

    DEFINES += SPEECH_LIBRARY
    SOURCES += Speech.cpp
    RootSpeech.cpp
    HEADERS += Speech.h
    Speech_global.h
    RootSpeech.h
    @

    For Gerolf answer I got it, and i think that will solve the problem. Currently I dont have time to test it, so I will mark thread with [Solved] and if i still have problems i will re edit this one.

    Thanks for help!



  • where define CORE_LIBRARY in the .pro file?



  • add to pro fie: @DEFINES += CORE_LIBRARY@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.