Issue setting QLibrary path
-
Hi,
I have a DLL in the same folder as my application's executable and all works fine when I load QLibrary with the path from QDir::currentPath(). However, I want to be able to use the command prompt and call my application that will be put in a subfolder within the current working directory that is calling the application. In that case, the library is not found.
I tried to get the DLL path using QCoreApplication::applicationDirPath() and then I noticed a strange behavior in QLibrary error message box when running from QtCreator:- In one hand, supposing I have the executable in ...\Desktop\MyApp\debug\app.exe and the DLL in that same directory, the error says that it cannot find the library in ...\Desktop\MyApp
- On the other hand, if I put the DLL in ...\Desktop\MyApp the message says that it cannot find the library in ...\Desktop\MyApp\debug
- In both cases the output of QCoreApplication::applicationDirPath() is always ...\Desktop\MyApp\debug
Any help?
Thanks!
-
Hi,
Can you show how you are using QLibrary ?
-
Like this:
mylib.h
#include <QLibrary> class MyLib : public QLibrary { public: MyLib(QObject *parent = nullptr); };
mylib.cpp
#include "mylib.h" #include <QCoreApplication> MyLib::MyLib(QObject *parent) : QLibrary(QCoreApplication::applicationFilePath().replace(QCoreApplication::applicationFilePath().split("/").last(),"myLib"),parent) { load(); }
-
Since you have the library in the same folder as your executable, what do you get if you just use the library name ?
-
In that case the app runs just fine. The problem is that I want to run the app from outside that folder but keeping the library there.
-
Since you are using QCoreApplication::applicationFilePath, did you check that value ? I would expect it to be consistant with applicationDirPath but there might be surprises.
-
Since you are using QCoreApplication::applicationFilePath, did you check that value ? I would expect it to be consistant with applicationDirPath but there might be surprises.
@SGaist Yes, I checked applicationDirPath() and applicationFilePath() and they are consistent with each other. The issue seems to be somehow in the way QLibrary is interpreting the path.
-
Do you get a canonical path ?
-
@SGaist What do you mean by "canonical"? I get absolute paths.
-
It's an absolute path with all the .. and . resolved cleanly.
-
@SGaist yes, I get full absolute paths.
-
Just to be sure, you have that behaviour when calling your application form the command line ?
-
Just to be sure, you have that behaviour when calling your application form the command line ?
@SGaist this behaviour happens with applicationFilePath() in QtCreator and also when running from the command line outside the executable's directory. If I call it in the executable's directory, all works fine.
-
Can you provide a minimal compilable example that shows this behaviour ?
-
@SGaist I was setting up an example for you and I found out that it works as I intended. In the example I can now run the app from the command line with the executable and its library in another folder.
I have to investigate better what could be the issue in my application... I will be back here if I get new questions.
Thank you! -
@SGaist Just solved the problem. I forgot that I had another class loading the library where I didn't change the constructor in order to use applicationFilePath() with replace and split... my bad.
Thank you again! -
If you have several places like that, you might want to use a common base class to avoid that kind of situation.
Anyway, glad you found out and thanks for sharing !
Since you have it working now, please mark the thread as solved using the "Topic Tools" button or the three dotted menu beside the answer you deem correct so that other forum users may know a solution has been found :-)
-
@SGaist Just solved the problem. I forgot that I had another class loading the library where I didn't change the constructor in order to use applicationFilePath() with replace and split... my bad.
Thank you again!@Tiago-M-Pinto said in Issue setting QLibrary path:
Just solved the problem
so don't forget to mark your post as solved!