How to use 3-party .dll and .lib libraries on windows



  • thoes libraries consists .dll .lib .h files, supposedly developed in an microsoft visual studio environment. And my qt is a newest 5.7 version, turns out the mingw compiler tool-chain can not recognize the .lib static library to my main program file, as a result I get "cant find reference" error for all the classes and functions defined by the .lib and .dll file. Can anybody give me some hint on How to compile and link this library in qt?


  • Qt Champions 2016

    Hello @sun-zu,
    You must use the same compiler as the one employed for the dll/lib file. Compilers are not interchangeable and might not even be compatible between versions, so you have to match the version of the compiler as well.

    Kind regards.



  • Thank you, I realise that, what troubling me more is that even I can't get QT5.7 to use the windows sdk 7.1 I have installed on my computer (which is compatible with codeblocks). That way, my problem falls on to how to setup gt5 so that I can choose windows sdk as its compiling toolchain? I can't find the documentation on related topics.


  • Qt Champions 2016



  • If the third party library has all the symbols exported in C (not C++ mangled) then you have a couple of options for dynamically linking to your program:

    • Use windows specific API's

    • Use QLibrary

    In Win32 it would be something like this:

    	typedef BOOL (__cdecl *Some_Exported_Function)(DWORD); // signature. returns MS bool and takes DWORD parameter
    	
    	Some_Exported_Function	some_function;
    	HINSTANCE hInstance_dll = LoadLibrary(L"name_of_library.dll");
    	some_function = (Some_Exported_Function) GetProcAddress(hInstance_dll, "Some_Function");
    
    	if(FALSE == (some_function)(123))
    	{
    		// returned false;
    	}
    	FreeLibrary(hInstance_dll);
    

    Using QLibrary (from the Qt help):

    	QLibrary myLib("mylib");
    	typedef void (*MyPrototype)();
    	MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
    	if (myFunction)
    		myFunction();
    	myLib.unload();
    

    The compiler won't matter in this case. I have no issues using this method to run functions compiled in a DLL from an older MSVC system (2003?) and accessing with a newer version of MinGW. This assumes the function names in the DLL are not mangled (if it is a third party library they probably shouldn't be).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.