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. Please help me understand how shared libraries work. (As in how to compile and import them in a project)
Forum Updated to NodeBB v4.3 + New Features

Please help me understand how shared libraries work. (As in how to compile and import them in a project)

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 385 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.
  • C Offline
    C Offline
    Curtwagner1984
    wrote on last edited by Curtwagner1984
    #1

    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 named Echo which has the function QString echo(QString txt) and it also has another class named Fibonacci that has the function GetFibonacciSequenceValueInPlace(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 or Fibonacci when I need them. But a shared library will be a single dll that includes both of those classes.

    jsulmJ 1 Reply Last reply
    0
    • C Curtwagner1984

      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 named Echo which has the function QString echo(QString txt) and it also has another class named Fibonacci that has the function GetFibonacciSequenceValueInPlace(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 or Fibonacci when I need them. But a shared library will be a single dll that includes both of those classes.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • C Offline
        C Offline
        Curtwagner1984
        wrote on last edited by
        #3

        Thank you for your reply. My apps are set like this:

        /
        --> app1
        --> lib1
        --> lib2

        /
        --> app2
        --> lib1
        --> libA
        --> libB

        And lib1 is common to both app2 and app1. So the question is should I taka lib1 outside?

        jsulmJ 1 Reply Last reply
        0
        • C Curtwagner1984

          Thank you for your reply. My apps are set like this:

          /
          --> app1
          --> lib1
          --> lib2

          /
          --> app2
          --> lib1
          --> libA
          --> libB

          And lib1 is common to both app2 and app1. So the question is should I taka lib1 outside?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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

          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