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. Using an existing DLL/Lib with Qt
QtWS25 Last Chance

Using an existing DLL/Lib with Qt

Scheduled Pinned Locked Moved General and Desktop
13 Posts 7 Posters 83.2k 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.
  • M Offline
    M Offline
    matthazley
    wrote on last edited by
    #1

    I have been trying to get a clear answer on this all morning but I haven't had much luck - maybe I'm being slow as I haven't used this functionality before.

    I have a .dll file and also a corresponding .lib file of the same name. I want to use some functions from these with my Qt application. I have a list of the functions available to me in the library and I also have a list of C++ prototypes for these functions (these are written using extern "C").

    From what I can understand from today ( might be wrong ) I can either load the library at runtime and attempt to resolve the function I need OR I can load the library at compile time, ensuring that I include the C++ prototypes in my header.

    Now, my first question is am I right in thinking those two things?

    If I am, is there a preferred way out of these?

    If I am wrong - would anyone be able to give me a gentle shove in the right direction?

    Thanks

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      I think your understanding is generally right. I usually prefer to use headers declaring the prototypes over dynamic runtime loading (e.g. through QLibrary). This is of course easiest if you have the header files provided by the author of the library you want to use.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        matthazley
        wrote on last edited by
        #3

        Yeah - I figured since I had the C++ prototypes I wouldnt have to worry about Dynamic runtime loading. Tell me - do you just include the library in the .pro file?

        Something like - win32:LIBS += path/to/myLib.lib

        I'll give it a shot - better to experiment and learn!

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on last edited by
          #4

          You still have the choice between dynamic and static linkage to your application (static is the .lib file, dynamic the .dll file). If you use static linking the file will be part of your app and you do not have to deploy additional files as you would with the dynamic lib (although you probably link Qt dynamically anyway, so you are probably familiar with that). If it is a popular library or if there are license issues (e.g. the library is LGPL) I would suggest you use dynamic linking otherwise static should be fine to.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            romankr
            wrote on last edited by
            #5

            you should

            1. add to *.pro file something like that
              @LIBS += d:/test/lib/somelib.lib@
            2. add to *.cpp
              @QLibrary library("somelib");
              library.load();@
            1 Reply Last reply
            0
            • L Offline
              L Offline
              loladiro
              wrote on last edited by
              #6

              A .lib file is a static library, you cannot use QLibrary to access it.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on last edited by
                #7

                I would use normal linking instead of QLibrary. That is way more straight forward as your OS will handle all the error cases even before your application starts. QLibrary only makes sense if you need to have an optional dependency on some libary I think.

                Plus your pro file should already link against somelib with the LIBS line, so you actually do not need to load it again using QLibrary... Use depends.exe (available somewhere on the microsoft website) to check the executable:-)

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  ludde
                  wrote on last edited by
                  #8

                  Some confusion here... On Windows, you always link against a .lib file, even if the library itself is in a DLL. What you normally have to do to use an external library, on any platform, is:

                  1. Add the library to the LIBS variable in your .pro file:
                    @LIBS += -llibrary@
                  2. Add the library path to the LIBS variables in your .pro file:
                    @LIBS += -LC:/path/to/library@
                  3. Add the include path to the INCLUDEPATH variable in your .pro file:
                    @INCLUDEPATH += C:/path/to/includes@

                  Then, if the library is a DLL, you must make sure the DLL is found when you run the program. On Windows you can e.g. add it the same directory as your executable. If you run the application from Qt Creator, it seems to be able to locate the DLL itself (if it's in the same place as the .lib file, I would guess).

                  1 Reply Last reply
                  3
                  • M Offline
                    M Offline
                    matthazley
                    wrote on last edited by
                    #9

                    Thanks guys - all solid info!

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      [quote author="Tobias Hunger" date="1308582243"]Use depends.exe (available somewhere on the microsoft website) to check the executable:-)[/quote]

                      It's available on http://www.dependencywalker.com/ nowadays.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        loladiro
                        wrote on last edited by
                        #11

                        [quote author="ludde" date="1308583463"]Some confusion here... On Windows, you always link against a .lib file, even if the library itself is in a DLL.
                        [/quote]

                        I never had to link .lib files (neither did I encounter them in my day-to-day development on windows). However I use MinGW. Is this different for MSVC?

                        EDIT: ok, I figured it out. The .lib extension is used for both, static libraries and import libraries. MinGw can link against dlls and (usually) doesn't need import libraries while MSVC apparently does.

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          jim_kaiser
                          wrote on last edited by
                          #12

                          @ludde: +1

                          Just to clear it up.. You can choose to link statically or dynamically, statically meaning the library is bundled along with your app, dynamic uses a dll. Now, if you have a .lib and a .dll, you have a library made to be used dynamically. You cannot use that .lib file statically, the static .lib is generally named (<somename>_a.lib), is of a bigger size than the .lib in the case of dynamic libraries. This is because the static .lib has to contain all the implementation, while in the dynamic .lib, you only have the exported symbols, and the actual implementation is in the dll.

                          @matthazley: You have a .lib and .dll but do you also have the header files (.h).. that is the general way to link to a library. You include the header in your code and use the methods. Compile with linking to the .lib. At runtime this loads the dll which has the implementation. Refer to @ludde for the definitions in the pro file.

                          1 Reply Last reply
                          1
                          • M Offline
                            M Offline
                            matthazley
                            wrote on last edited by
                            #13

                            Cheers Jim.

                            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