Difference in relative file path: debug mode vs release mode of Qt Creator
QFile file("test.txt");
if (file.open(QIODevice::ReadOnly)) {
qDebug()<<"You got me.";
}
I use:
- Qt 4.8.6 with MSVC 2010
- Qt Creator 3.1.1
- Windows 7 (32 bit)
From the above code , if the .pro
file has not been modified , the corresponding build directory is
for debug mode :
D:\...\build-Main-MSVC2010-Debug
and .exe
the debug mode will be in
D:\...\build-Main-MSVC2010-Debug\debug
for release mode :
D:\...\build-Main-MSVC2010-Release
and .exe
the release mode will be in
D:\...\build-Main-MSVC2010-Release\release
[Question]
If I want the release program to read the file test.txt, I put the file in the folder
D:\...\build-Main-MSVC2010-Release\release
which makes sense.
But if I want the program to debug read the file test.txt, I have to put the file in the folder
D:\...\build-Main-MSVC2010-Debug\
but not
D:\...\build-Main-MSVC2010-Debug\debug
I'm wondering why relative filepath worked differently in debug and release mode, it bothered me for a long time.
[change]
Thanks for @Paul and @lpapp. Here is a screenshot of the working directory:
Debug:
Release:
[Very important to change]
For @Paul and @lpapp:
I used to copy the required one .dll
to the release folder to check the runtime and I just found if I run the release program through Qt Creator the working directory works the same as what you said, But if I just click on. exe in the release folder, the situation will be as I said in the question. I think there might be differences between running a program from Qt Creator and directly executing the program.
source to share
It depends on the current working directory of your program. You can change it in Project-> Run settings-> Run-> Working directory.
If I run the release program through Qt Creator, the working directory works the same as you said. But if I just click on the .exe in the release folder the situation is as I said in the question.
This is because when you click on the .exe in the foder, this folder is the working directory for the program. When you run a program from Qt Creator, Qt Creator explicitly sets the working directory. You can override the working directory as the directory where the .exe file is created, so it makes no difference if you launch the program from Qt Creator or just click the .exe file in explorer.
source to share