How-to reduce required Internal/External Headers/DLLs when creating a Shared Library?
-
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
andOpenCV
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.hclass 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.htemplate <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.hclass 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.hnamespace 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 getundefined 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 ? -
@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.