Forward Declaration in a Static Library



  • Hello,

    I am trying to compile a static library to use with a non-Qt application. I wish to create a wrapper in this library to minimize the number of includes the final static library needs.

    These are the basics of the code:

    @
    //libraryWrapper.h
    class ImageSender; //forward declaration

    class LibraryWrapper{

    public:
    LibraryWrapper();
    ~LibraryWrapper();

    private:
    ImageSender* _sender;
    }
    @

    The library source file is:

    @
    //LibraryWrapper.cpp
    #include "LibraryWrapper.h"
    #include "imagesender.h"

    LibraryWrapper::LibraryWrapper(){
    _sender = new ImageSender();
    }

    ~ImageSender(){
    //delete _sender;
    }
    @

    I successfully compile this and can link to it in my non-qt project. If I comment out _sender = new ImageSender(); (line 6 in LibraryWrapper.cpp), I do not get any errors when I compile and run the non-qt application.

    @
    //LibraryWrapper.pro
    QT += network
    QT -= gui

    TEMPLATE = lib
    CONFIG += staticlib

    HEADERS += imageudpsenderwrapper.h \

    SOURCES += imageudpsenderwrapper.cpp \

    TARGET = ../../../ShutterSoftware/build/ImageUdpSenderLibrary
    @

    The only thing I can think of is adding imagesender.h to the HEADERS += in the .pro file, but when I do this it asks for its dependencies, which I think defeats the purpose of the forward declaration....

    Just to show the application linking to the static library:

    @
    #include "stdafx.h"
    #include "imageudpsenderwrapper.h"

    using namespace System;

    int main(array<System::String ^> ^args)
    {
    ImageUdpSenderWrapper* _sender = new ImageUdpSenderWrapper();
    return 0;
    }
    @

    Thanks!


  • Lifetime Qt Champion

    Hi,

    Building your library requires all headers to be listed and that's correct.

    However when using it in your application you'll only use imageudpsenderwrapper.h which is want you want to do.

    Unless I misunderstood your problem



  • Thanks for your help. My issue was omitting the library headers in the .pro.


  • Lifetime Qt Champion

    You're welcome !

    Now that you have it compiling, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

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