Program cannot find image directory when run from IDE, but CAN when run from folder?

旧城冷巷雨未停 提交于 2019-12-13 05:15:38

问题


When I build, it succeeds with no errors. However, when I run from the IDE, I get an error (my image fails to load because it cannot find the directory).

However, if I go into the folder and run the program(.exe) it finds the image directory perfectly.

mTextures.Load(Textures::Background, "../GFX/Background.png");

^the line of code giving the directory.

I assume this is a problem with a setting I didn't enter correctly in my compiler?

I am using Code::Blocks on Windows.


回答1:


Your debugger's current directory (i.e. the current directory used when you execute the application from within codeblock) is probably incorrect. Check your project settings, and fix the current directory to your target directory (the one which contains the executable itself).

Specific instructions are here.




回答2:


It's because you're using a relative path in your file name:

"../GFX/Background.png"

the .. is saying "go up one directory form the current directory". If you want to be able to run your program anywhere, use an absolute path, something like:

"/home/me/GFX/Background.png"



回答3:


Add the full path where the image exist with double slash.

mTextures.Load(Textures::Background, "C:\\Program Files\\..\\..\\GFX\\Background.png");


来源:https://stackoverflow.com/questions/25937305/program-cannot-find-image-directory-when-run-from-ide-but-can-when-run-from-fol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!