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?
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. add to *.pro file something like that
- LIBS += d:/test/lib/somelib.lib
2. add to *.cpp
- QLibrary library("somelib");
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:-)
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).
Use depends.exe (available somewhere on the microsoft website) to check the executable:-)
It’s available on http://www.dependencywalker.com/ nowadays.
DLL.Some confusion here… On Windows, you always link against a .lib file, even if the library itself is in a
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.
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.