Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How-to reduce required Internal/External Headers/DLLs when creating a Shared Library?

How-to reduce required Internal/External Headers/DLLs when creating a Shared Library?

Scheduled Pinned Locked Moved Unsolved General and Desktop
libraryshared libraryopencv
5 Posts 2 Posters 559 Views
  • 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.
  • R Offline
    R Offline
    R-P-H
    wrote on 6 Apr 2023, 13:19 last edited by R-P-H 4 Jun 2023, 13:51
    #1

    Hi, I am using Qt to create a shared library DLL file that I can use in other projects. Inside my main header file I include a few other classes that I created as part of my project as well as other external headers such as QtSql and OpenCV for example.

    Headers:

    To reduce the required headers when using the library in other projects I changed:
    main.h

    #include "classA"
    #include "classB"
    
    class MYCLASS_EXPORT myClass
    {
    public:
        myClass();
        classA* A;
        classB* B;
    };
    

    to
    main.h

    class A;
    class B;
    class MYCLASS_EXPORT myClass
    {
    public:
        myClass();
        classA* A;
        classB* B;
    };
    

    main.cpp

    #include "classA"
    #include "classB"
    ...
    

    which seems to work fine when I now include just main.h inside other projects.

    However, how can I do the same for template based classes such as:
    classC.h

    template <typename T>
    class classC
    {
    public:
        classC();
        T myNum;
    };
    

    main.h

    #include "classC"
    
    class A;
    class B;
    class MYCLASS_EXPORT myClass
    {
    public:
        myClass();
        classA* A;
        classB* B;
        classC<int> myResult(int myInput);
    };
    

    and other external library headers such as OpenCV:
    main.h

    #include <opencv2/opencv.hpp>
    
    class A;
    class MYCLASS_EXPORT myClass
    {
    public:
        myClass();
        classA* A;
        cv::Mat myResult(cv::Mat myInputMat);
    };
    

    If I do the same forward declaration for the template class:
    main.h

    class A;
    class B;
    template <typename T> class classC;
    class MYCLASS_EXPORT myClass
    {
    public:
        myClass();
        classA* A;
        classB* B;
        classC<int> myResult(int myInput);
    };
    

    main.cpp

    #include "classC.h"
    ...
    

    I get an undefined error when using the function in other projects?

    For OpenCV, if I try something like:
    main.h

    namespace cv{
        auto Mat;
    }
    class MYCLASS_EXPORT myClass
    {
    public:
        myClass();
        cv::Mat myResult(cv::Mat myInputMat);
    };
    

    main.cpp

    #include <opencv2/opencv.hpp>
    ...
    

    then I also get an error when using the function in the other project?

    DLLs

    In terms of DLLs, I have linked OpenCV and other 3rd Party libs in my .pro file. When building my DLL and testing the library in another project none of the linked libraries are included inside my DLL so I get undefined reference errors. Does this mean I have to still manually link and ship the 3rd party DLLs along with my own DLL even though I had linked them in my original project ?

    J 1 Reply Last reply 6 Apr 2023, 13:27
    0
    • R R-P-H
      6 Apr 2023, 13:19

      Hi, I am using Qt to create a shared library DLL file that I can use in other projects. Inside my main header file I include a few other classes that I created as part of my project as well as other external headers such as QtSql and OpenCV for example.

      Headers:

      To reduce the required headers when using the library in other projects I changed:
      main.h

      #include "classA"
      #include "classB"
      
      class MYCLASS_EXPORT myClass
      {
      public:
          myClass();
          classA* A;
          classB* B;
      };
      

      to
      main.h

      class A;
      class B;
      class MYCLASS_EXPORT myClass
      {
      public:
          myClass();
          classA* A;
          classB* B;
      };
      

      main.cpp

      #include "classA"
      #include "classB"
      ...
      

      which seems to work fine when I now include just main.h inside other projects.

      However, how can I do the same for template based classes such as:
      classC.h

      template <typename T>
      class classC
      {
      public:
          classC();
          T myNum;
      };
      

      main.h

      #include "classC"
      
      class A;
      class B;
      class MYCLASS_EXPORT myClass
      {
      public:
          myClass();
          classA* A;
          classB* B;
          classC<int> myResult(int myInput);
      };
      

      and other external library headers such as OpenCV:
      main.h

      #include <opencv2/opencv.hpp>
      
      class A;
      class MYCLASS_EXPORT myClass
      {
      public:
          myClass();
          classA* A;
          cv::Mat myResult(cv::Mat myInputMat);
      };
      

      If I do the same forward declaration for the template class:
      main.h

      class A;
      class B;
      template <typename T> class classC;
      class MYCLASS_EXPORT myClass
      {
      public:
          myClass();
          classA* A;
          classB* B;
          classC<int> myResult(int myInput);
      };
      

      main.cpp

      #include "classC.h"
      ...
      

      I get an undefined error when using the function in other projects?

      For OpenCV, if I try something like:
      main.h

      namespace cv{
          auto Mat;
      }
      class MYCLASS_EXPORT myClass
      {
      public:
          myClass();
          cv::Mat myResult(cv::Mat myInputMat);
      };
      

      main.cpp

      #include <opencv2/opencv.hpp>
      ...
      

      then I also get an error when using the function in the other project?

      DLLs

      In terms of DLLs, I have linked OpenCV and other 3rd Party libs in my .pro file. When building my DLL and testing the library in another project none of the linked libraries are included inside my DLL so I get undefined reference errors. Does this mean I have to still manually link and ship the 3rd party DLLs along with my own DLL even though I had linked them in my original project ?

      J Offline
      J Offline
      JonB
      wrote on 6 Apr 2023, 13:27 last edited by JonB 4 Jun 2023, 14:43
      #2

      @R-P-H said in How-to reduce required Internal/External Headers/DLLs when creating a Shared Library?:

      Does this mean I have to still manually link and ship the 3rd party DLLs along with my own DLL even though I had linked them in my original project ?

      Yes. Your "linking" DLLs does not really mean that, DLLs do not get included only satisfy references, they have to be present at run-time. Same with the Qt DLLs themselves.

      R 1 Reply Last reply 6 Apr 2023, 13:43
      3
      • J JonB
        6 Apr 2023, 13:27

        @R-P-H said in How-to reduce required Internal/External Headers/DLLs when creating a Shared Library?:

        Does this mean I have to still manually link and ship the 3rd party DLLs along with my own DLL even though I had linked them in my original project ?

        Yes. Your "linking" DLLs does not really mean that, DLLs do not get included only satisfy references, they have to be present at run-time. Same with the Qt DLLs themselves.

        R Offline
        R Offline
        R-P-H
        wrote on 6 Apr 2023, 13:43 last edited by
        #3

        @JonB Noted thanks. So I would have to include all Qt and OpenCV DLLs along with my built DLL as well.

        J 1 Reply Last reply 6 Apr 2023, 14:45
        0
        • R R-P-H
          6 Apr 2023, 13:43

          @JonB Noted thanks. So I would have to include all Qt and OpenCV DLLs along with my built DLL as well.

          J Offline
          J Offline
          JonB
          wrote on 6 Apr 2023, 14:45 last edited by
          #4

          @R-P-H
          Yep. Many people distribute Windows Qt applications by using windeployqt, that packages all the Qt DLLs with your app, I don't know but hopefully you can add 3rd party stuff like OpenCV to its manifest.

          R 1 Reply Last reply 7 Apr 2023, 07:19
          0
          • J JonB
            6 Apr 2023, 14:45

            @R-P-H
            Yep. Many people distribute Windows Qt applications by using windeployqt, that packages all the Qt DLLs with your app, I don't know but hopefully you can add 3rd party stuff like OpenCV to its manifest.

            R Offline
            R Offline
            R-P-H
            wrote on 7 Apr 2023, 07:19 last edited by
            #5

            @JonB Thanks, I normally use CQTDeployer. That would mean for other projects the DLLs also need to be added under LIBS in the .pro file ?

            1 Reply Last reply
            0

            3/5

            6 Apr 2023, 13:43

            • Login

            • Login or register to search.
            3 out of 5
            • First post
              3/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved