Cannot load dll with QLibrary using Qt5
-
Hello @artwaw, thank you for the help.
I printed the searching locations as you said and my program is searching in my build directory where I have the DLLs. So, I am still wondering what's wrong... -
@Tiago-M-Pinto
Please collapse all the EXT-MS-* and API-MS-* entries. Then you'll see the real problematic ones.
How can I know which dependecies are these? Do I have to provide the path for them? How?
With Dependency Walker, e.g. Either copy the deps to the program or provides them with the
PATH
variable.Btw., for deployment please use https://doc.qt.io/qt-5/windows-deployment.html
Regards
Please collapse all the EXT-MS-* and API-MS-* entries. Then you'll see the real problematic ones.
After searching for errors different than EXT-MS-* and API-MS-*, I found that hvsifiletrust.dll and IEShims.dll were missing. I do not know if they could be ignored so I downloaded them from the web and put them in my program directory but it still does not work.
-
@Tiago-M-Pinto The only thing that comes to my mind is that the lib version (or type, confused 32/64bit) is mismatched. Maybe some more experienced member of the community will be able to provide more help?
@artwaw I just found out that my 3rd party DLLs were compiled using MS Visual Studio (probably MSVC). Could this be the problem since I am compiling my program with MinGW?
-
@artwaw I just found out that my 3rd party DLLs were compiled using MS Visual Studio (probably MSVC). Could this be the problem since I am compiling my program with MinGW?
-
@Tiago-M-Pinto Yes. They need to be recompiled with minGW or you should compile your program using MSVS.
@artwaw OK, thank you. I will try to do that. However, I was using the same DLLs before while compiling my program with MinGW and everything worked just fine. How could that be?
-
@artwaw OK, thank you. I will try to do that. However, I was using the same DLLs before while compiling my program with MinGW and everything worked just fine. How could that be?
@Tiago-M-Pinto Can't say. There must be differences in how the library exposes the data but I have very little knowledge on how exactly they differ - I tried MSVC once and ran away screaming, minGW is so much easier on Windows. Me myself I always use minGW for everything on Windows and it just... works.
-
@artwaw I just found out that my 3rd party DLLs were compiled using MS Visual Studio (probably MSVC). Could this be the problem since I am compiling my program with MinGW?
@Tiago-M-Pinto said in Cannot load dll with QLibrary using Qt5:
@artwaw I just found out that my 3rd party DLLs were compiled using MS Visual Studio (probably MSVC). Could this be the problem since I am compiling my program with MinGW?
You didn't say if the 3rd-party code is written in C or C++.
If they are C++ DLLs, then they must use compatible compilers because of Name Mangling.
C DLLs don't have name mangling so they can be mixed.
I put the dlls in the build directory and the corresponding errorString returns: "Cannot load library xxxxxx: The specified module could not be found."
You said you "put the DLLs in the build directory"...
- What is the absolute path of that dirrectory?
- What path did you give to QLibrary?
- What does
QDir::currentPath()
give you right before you initialize your QLibrary?
Finally, what are the DLLs that you're loading? Do you really need QLibrary? Could you link the DLLs directly to your executable instead?
-
@Tiago-M-Pinto said in Cannot load dll with QLibrary using Qt5:
@artwaw I just found out that my 3rd party DLLs were compiled using MS Visual Studio (probably MSVC). Could this be the problem since I am compiling my program with MinGW?
You didn't say if the 3rd-party code is written in C or C++.
If they are C++ DLLs, then they must use compatible compilers because of Name Mangling.
C DLLs don't have name mangling so they can be mixed.
I put the dlls in the build directory and the corresponding errorString returns: "Cannot load library xxxxxx: The specified module could not be found."
You said you "put the DLLs in the build directory"...
- What is the absolute path of that dirrectory?
- What path did you give to QLibrary?
- What does
QDir::currentPath()
give you right before you initialize your QLibrary?
Finally, what are the DLLs that you're loading? Do you really need QLibrary? Could you link the DLLs directly to your executable instead?
Hello @JKSH, thank you for your reply.
You didn't say if the 3rd-party code is written in C or C++.
The 3rd-party code is written in C and its header file is written in this way:
#ifdef __cplusplus extern "C" { #endif #ifdef DLLEXP_UTIL __declspec(dllexport) #endif void myFunc(void); #ifdef __cplusplus } #endif
I presume the keyword
__declspec(dllexport)
is from MSVC. If yes, the DLLs can only be loaded with an executable compiled with MSVC, right?Since I am using minGW compiler, If it was used to compile the DLLs, could I solve my problem?
- What is the absolute path of that dirrectory?
C:\Users\Tiago\Google Drive\Projects\build-project-Desktop_Qt_5_14_1_MinGW_64_bit-Release
- What path did you give to QLibrary?
To load the DLLs I only gave their name in the code becasue (as I already posted here) the directory above is included in the search paths.
- What does QDir::currentPath() give you right before you initialize your QLibrary?
It gives me the directory above.
Finally, what are the DLLs that you're loading? Do you really need QLibrary? Could you link the DLLs directly to your executable instead?
The DLLs have some functions that I need to use. My goal is to upgrade my program with no need to compile it again if new DLLs are available (the functions prototypes won't change). Is there any other library that I could use instead of QLibrary?
-
Hi,
If you want to use the export/import part, it's a bit more complicated than just __declspec. Take a look at the Creating Shared Libraries chapter in Qt's documentation.
-
Hello @JKSH, thank you for your reply.
You didn't say if the 3rd-party code is written in C or C++.
The 3rd-party code is written in C and its header file is written in this way:
#ifdef __cplusplus extern "C" { #endif #ifdef DLLEXP_UTIL __declspec(dllexport) #endif void myFunc(void); #ifdef __cplusplus } #endif
I presume the keyword
__declspec(dllexport)
is from MSVC. If yes, the DLLs can only be loaded with an executable compiled with MSVC, right?Since I am using minGW compiler, If it was used to compile the DLLs, could I solve my problem?
- What is the absolute path of that dirrectory?
C:\Users\Tiago\Google Drive\Projects\build-project-Desktop_Qt_5_14_1_MinGW_64_bit-Release
- What path did you give to QLibrary?
To load the DLLs I only gave their name in the code becasue (as I already posted here) the directory above is included in the search paths.
- What does QDir::currentPath() give you right before you initialize your QLibrary?
It gives me the directory above.
Finally, what are the DLLs that you're loading? Do you really need QLibrary? Could you link the DLLs directly to your executable instead?
The DLLs have some functions that I need to use. My goal is to upgrade my program with no need to compile it again if new DLLs are available (the functions prototypes won't change). Is there any other library that I could use instead of QLibrary?
@Tiago-M-Pinto said in Cannot load dll with QLibrary using Qt5:
If yes, the DLLs can only be loaded with an executable compiled with MSVC, right?
Usually,
extern "C"
means the DLL can be loaded with any executable compiled with any C/C++ compiler.Since I am using minGW compiler, If it was used to compile the DLLs, could I solve my problem?
The best way to find out is to try it.
The DLLs have some functions that I need to use. My goal is to upgrade my program with no need to compile it again if new DLLs are available (the functions prototypes won't change). Is there any other library that I could use instead of QLibrary?
It doesn't sound like you need a library to use the DLL. Just add the header and library to your *.pro file, and then
#include
the header in your .cpp code and call the function directly!Example: If you have these files...
- C:\my3rdpartystuff\include\foo.h
- C:\my3rdpartystuff\bin\foo.dll
...then add this to your *.pro file:
INCLUDEPATH += "C:/my3rdpartystuff/include" LIBS += -L"C:/my3rdpartystuff/bin" -lfoo
[EDIT: Trailing slashes removed from the 2 lines above]
If there are any problems, the compiler and linker will tell you.
-
@Tiago-M-Pinto said in Cannot load dll with QLibrary using Qt5:
If yes, the DLLs can only be loaded with an executable compiled with MSVC, right?
Usually,
extern "C"
means the DLL can be loaded with any executable compiled with any C/C++ compiler.Since I am using minGW compiler, If it was used to compile the DLLs, could I solve my problem?
The best way to find out is to try it.
The DLLs have some functions that I need to use. My goal is to upgrade my program with no need to compile it again if new DLLs are available (the functions prototypes won't change). Is there any other library that I could use instead of QLibrary?
It doesn't sound like you need a library to use the DLL. Just add the header and library to your *.pro file, and then
#include
the header in your .cpp code and call the function directly!Example: If you have these files...
- C:\my3rdpartystuff\include\foo.h
- C:\my3rdpartystuff\bin\foo.dll
...then add this to your *.pro file:
INCLUDEPATH += "C:/my3rdpartystuff/include" LIBS += -L"C:/my3rdpartystuff/bin" -lfoo
[EDIT: Trailing slashes removed from the 2 lines above]
If there are any problems, the compiler and linker will tell you.
@JKSH, that is a very good post!
Just one addition:
INCLUDEPATH += "C:/my3rdpartystuff/include/"
LIBS += -L"C:/my3rdpartystuff/bin/" -lfooIt might be useful to omit the trailing slashes for the directories.
INCLUDEPATH += "C:/my3rdpartystuff/include" LIBS += -L"C:/my3rdpartystuff/bin" -lfoo
On Windows these are converted to backslashes and can cause problems on line ends, for example.
Regards
-
@Tiago-M-Pinto said in Cannot load dll with QLibrary using Qt5:
If yes, the DLLs can only be loaded with an executable compiled with MSVC, right?
Usually,
extern "C"
means the DLL can be loaded with any executable compiled with any C/C++ compiler.Since I am using minGW compiler, If it was used to compile the DLLs, could I solve my problem?
The best way to find out is to try it.
The DLLs have some functions that I need to use. My goal is to upgrade my program with no need to compile it again if new DLLs are available (the functions prototypes won't change). Is there any other library that I could use instead of QLibrary?
It doesn't sound like you need a library to use the DLL. Just add the header and library to your *.pro file, and then
#include
the header in your .cpp code and call the function directly!Example: If you have these files...
- C:\my3rdpartystuff\include\foo.h
- C:\my3rdpartystuff\bin\foo.dll
...then add this to your *.pro file:
INCLUDEPATH += "C:/my3rdpartystuff/include" LIBS += -L"C:/my3rdpartystuff/bin" -lfoo
[EDIT: Trailing slashes removed from the 2 lines above]
If there are any problems, the compiler and linker will tell you.
@JKSH, thank you again.
The best way to find out is to try it.
I compiled a simple DLL using minGW and it finally worked using QLibrary.
It doesn't sound like you need a library to use the DLL. Just add the header and library to your *.pro file, and then #include the header in your .cpp code and call the function directly!
You are right, I will do that. I didn't think about it but it... that way I can avoid resolves using QLibrary.
-
@Tiago-M-Pinto said in Cannot load dll with QLibrary using Qt5:
If yes, the DLLs can only be loaded with an executable compiled with MSVC, right?
Usually,
extern "C"
means the DLL can be loaded with any executable compiled with any C/C++ compiler.Since I am using minGW compiler, If it was used to compile the DLLs, could I solve my problem?
The best way to find out is to try it.
The DLLs have some functions that I need to use. My goal is to upgrade my program with no need to compile it again if new DLLs are available (the functions prototypes won't change). Is there any other library that I could use instead of QLibrary?
It doesn't sound like you need a library to use the DLL. Just add the header and library to your *.pro file, and then
#include
the header in your .cpp code and call the function directly!Example: If you have these files...
- C:\my3rdpartystuff\include\foo.h
- C:\my3rdpartystuff\bin\foo.dll
...then add this to your *.pro file:
INCLUDEPATH += "C:/my3rdpartystuff/include" LIBS += -L"C:/my3rdpartystuff/bin" -lfoo
[EDIT: Trailing slashes removed from the 2 lines above]
If there are any problems, the compiler and linker will tell you.
@JKSH just one more thing:
I couldn't add my DLL as you said but all worked out by adding the library using Qt Creator interface (in "Add Library..." -> "External Library") which asks for the .a file and the include path.
-
@JKSH, that is a very good post!
Just one addition:
INCLUDEPATH += "C:/my3rdpartystuff/include/"
LIBS += -L"C:/my3rdpartystuff/bin/" -lfooIt might be useful to omit the trailing slashes for the directories.
INCLUDEPATH += "C:/my3rdpartystuff/include" LIBS += -L"C:/my3rdpartystuff/bin" -lfoo
On Windows these are converted to backslashes and can cause problems on line ends, for example.
Regards
@Tiago-M-Pinto said in Cannot load dll with QLibrary using Qt5:
I compiled a simple DLL using minGW and it finally worked using QLibrary.
...
I couldn't add my DLL as you said but all worked out by adding the library using Qt Creator interface (in "Add Library..." -> "External Library") which asks for the .a file and the include path.
Sounds like you got it to work in 2 different ways. Congrats, and happy coding!
@aha_1980 said in Cannot load dll with QLibrary using Qt5:
It might be useful to omit the trailing slashes for the directories.
...
On Windows these are converted to backslashes and can cause problems on line ends, for example.
Edited, thanks!