Please help me understand how shared libraries work. (As in how to compile and import them in a project)
-
Hello,
Disclaimer: Let me preface this with the fact that I've read the documentation found here.
The issue I'm having is the following:
I have two applications that use the same code module let's call it ModuleX. Currently each of the applications have a copy of ModuleX as a sub-project inside it. I want to avoid this and ideally I want to compile ModuleX as a sperate project into a dll that both applications can then import and link to.I'm having trouble figuring out how I should structure my projects and pro files to achieve this.
The goal is to avoid code duplication so MoudleX's project should be placed in a separate folder and both applications should link to a compiled version of ModuleX? Does this also mean that each of the applications need to have a copy of the ModuleX header files? Isn't that code duplication too?Also, who is a shared library fundamentally different from plugin architecture described here?
Is the difference in the fact that plug-ins must implement a specific interface?Ideally I want ModuleX to be like a python module where you just install it and import it and you can use it in your code.
PS:
It's entirely possible that I don't understand or miss something. I never had to create shared libraries in C++ before.EDIT:
Also, what happens if ModuleX has multiple classes that I want to use in the applications. For example, if ModuleX has a class namedEcho
which has the functionQString echo(QString txt)
and it also has another class namedFibonacci
that has the functionGetFibonacciSequenceValueInPlace(int sequencePlace )
How would I expose those function to the application with shared libraries? Currently what I do is just include the header for
Echo
orFibonacci
when I need them. But a shared library will be a single dll that includes both of those classes. -
@Curtwagner1984 It sounds like https://wiki.qt.io/SUBDIRS_-_handling_dependencies is what you need. You can organise your projects like:
/ --> app1 --> app2 --> lib
Both apps can then reference (link against) lib.
"How would I expose those function to the application with shared libraries?" - you need to export all symbols in your lib you want to access, see https://doc.qt.io/qt-5/sharedlibrary.html
-
Thank you for your reply. My apps are set like this:
/
--> app1
--> lib1
--> lib2/
--> app2
--> lib1
--> libA
--> libBAnd
lib1
is common to both app2 and app1. So the question is should I takalib1
outside? -
@Curtwagner1984 said in Please help me understand how shared libraries work. (As in how to compile and import them in a project):
So the question is should I taka lib1 outside?
Yes