Congratulations to our 2022 Qt Champions!

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:

    class ImageSender; //forward declaration

    class LibraryWrapper{


    ImageSender* _sender;

    The library source file is:

    #include "LibraryWrapper.h"
    #include "imagesender.h"

    _sender = new 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.

    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;


  • Lifetime Qt Champion


    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