PDA

View Full Version : Linking to a windows DLL from Qt Creator on Windows



barryhf
25th November 2009, 19:46
I have built an app in Qt Creator on Windows, and it builds and runs correctly.

I have received header, .lib and .dll from a vendor built with Visual Studio on Windows. I would like to link to to their libraries. I do not have access to their source code.

I'm using the g++ toolset and fail at link time (exported symbols, I assume). The vendor's VendorClass.h, VendorClass.lib and VendorClass.dll are all in "C:/Program Files/VendorTools/lib"

My .pro file has

INCLUDEPATH += "C:/Program Files/VendorTools/lib"
LIBS += -L"C:/Program Files/VendorTools/lib" -lVendorClass

I edit the vendor's header file so that it reads:

class Q_DECL_IMPORT VendorClass
{
public:
VendorClass();
}

(I've also tried declaring the class with __declspec(dllimport) ), same results.

Error is something like:

undefined reference to '__imp___ZN5VendorClassC1ERKSs'


dumpbin /linkermember VendorClass.lib shows a decorated class name that differs from the linker error I get from Qt.

How do I properly declare and compile a class so that it will link to a VC2008-compiled .lib for a .dll? Or is there a compiler flag that is required for properly using a Windows dll.

squidge
25th November 2009, 21:40
G++ uses different name decoration than Visual studio, so by default DLL files generated by Visual studio are not compatible with G++. They also has different runtime libraries and possibly even different ABI's.

If it was a C library, you can sometimes link against it with some tweaking, but being C++ presents a nightmare. You can create a shunt between the two interfaces, but thats not exactly easy.

Sometimes even a different version of the compiler isn't compatible with a DLL from a vendor, which is why they typically distribute the library for the different versions available.

This is why Microsoft use COM to access the C++ classes within Windows. It bypasses all of this and so you end with it not caring about the compiler, version, mangling type, etc.

By far the easiest way of using the library is to use the same version of Visual Studio that the vendor used.

barryhf
25th November 2009, 23:44
Thank you for the basic answer.:)

Another newbie follow-up: Is there a FAQ for configuring Qt Creator to use the Visual Studio compilers (which I have installed)?