简体   繁体   中英

Difference in relative file path: debug mode and release mode of Qt Creator

 QFile file("test.txt");
 if (file.open(QIODevice::ReadOnly)) {
     qDebug()<<"You got me.";
 }

I am using:

  • Qt 4.8.6 with MSVC 2010
  • Qt Creator 3.1.1
  • Windows 7 (32 bit)

From the above code, if .pro file has not been changed , the corresponding build directory

for the debug mode :

D:\...\build-Main-MSVC2010-Debug

and the .exe of the debug mode will be located in

D:\...\build-Main-MSVC2010-Debug\debug

for the release mode :

D:\...\build-Main-MSVC2010-Release

and the .exe of the release mode will be located in

D:\...\build-Main-MSVC2010-Release\release

[Question]

If I want the release program to read the "test.txt" file, I put the file in the folder

D:\...\build-Main-MSVC2010-Release\release

which makes sense.

But if I want the debug program to read the "test.txt" file, I have to put the file in the folder

D:\...\build-Main-MSVC2010-Debug\

rather than

D:\...\build-Main-MSVC2010-Debug\debug

I am wondering why the relative file path worked differently in debug & release mode, it has been bothering me for a long time.


[Edit]

Thanks for @Paul and @lpapp. Here is the screenshot of working directory:

Debug: 在此输入图像描述

Release: 在此输入图像描述


[Very important Edit]

For @Paul and @lpapp:

I used to copy required .dll to the release folder to test runtime, and I just found if I execute the release program through Qt Creator , the working directory will work just like what you said. But if I directly click the .exe in the release folder, the situation will be as what I said in the question. I think there might be differences between running program from Qt Creator and directly executing the program.

This depends on current working directory of your program. You can change it in Project->Run settings->Run->Working directory.

在此输入图像描述

if I execute the release program through Qt Creator, the working directory will work just like what you said. But if I directly click the .exe in the release folder, the situation will be as what I said in the question.

This is because when you click the .exe in the release foder, that folder is the working directory for the program. When you run the program from Qt Creator, Qt Creator sets the working directory explicitly. You may override the Working directory to be the directory where .exe file is created so it'll be no difference whether you run the program from Qt Creator or just click the .exe in Explorer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM