Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Forward Declaration in a Static Library
Forum Updated to NodeBB v4.3 + New Features

Forward Declaration in a Static Library

Scheduled Pinned Locked Moved Installation and Deployment
4 Posts 2 Posters 1.5k 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.
  • H Offline
    H Offline
    heliosbird
    wrote on last edited by
    #1

    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!

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

      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

      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
      • H Offline
        H Offline
        heliosbird
        wrote on last edited by
        #3

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

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

          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 :)

          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