June 20, 2011

matthazley matthazley
Lab Rat
6 posts

Using an existing DLL/Lib with Qt

 

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

12 replies

June 20, 2011

loladiro loladiro
Lab Rat
594 posts

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.

June 20, 2011

matthazley matthazley
Lab Rat
6 posts

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!

June 20, 2011

loladiro loladiro
Lab Rat
594 posts

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.

June 20, 2011

romankr romankr
Lab Rat
14 posts

you should
1. add to *.pro file something like that

  1. LIBS += d:/test/lib/somelib.lib

2. add to *.cpp
  1. QLibrary library("somelib");
  2. library.load();

June 20, 2011

loladiro loladiro
Lab Rat
594 posts

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

June 20, 2011

Tobias Hunger Tobias Hunger
Hobby Entomologist
3373 posts

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:-)

June 20, 2011

ludde ludde
Lab Rat
287 posts

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:

  1. LIBS += -llibrary

2) Add the library path to the LIBS variables in your .pro file:
  1. LIBS += -LC:/path/to/library

3) Add the include path to the INCLUDEPATH variable in your .pro file:
  1. 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).

June 20, 2011

matthazley matthazley
Lab Rat
6 posts

Thanks guys – all solid info!

June 20, 2011

Volker Volker
Ant Farmer
5331 posts
Tobias Hunger wrote:
Use depends.exe (available somewhere on the microsoft website) to check the executable:-)

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

June 20, 2011

loladiro loladiro
Lab Rat
594 posts
ludde wrote:
Some confusion here… On Windows, you always link against a .lib file, even if the library itself is in a DLL.

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.

June 21, 2011

jim_kaiser jim_kaiser
Lab Rat
144 posts

@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.

June 21, 2011

matthazley matthazley
Lab Rat
6 posts

Cheers Jim.

 
  ‹‹ [Solved] Qt always tries to use OpenGL ES2 under Win7 64-Bit      QTcpSocket with signal capability ››

You must log in to post a reply. Not a member yet? Register here!