使用不同目录中的映像执行的文件

Executable using images in different directory

本文关键字:映像 执行 文件      更新时间:2023-10-16

我有以下目录结构

       (root)
  /      |        
bin    resources  src
|        |         |
Debug  dot.bmp   .cpp
|
.exe

I would like the .exe file to use dot.bmp.

Here's the code from the .cpp file that loads dot.bmp

player_img = il->load_image( "dot.bmp" );

I feel like I need to modify this line of code, but after changing it to:

player_img = il->load_image( "../resources/dot.bmp" );

I still get an error saying that the image couldn't be loaded.

What do I need to change? Is this even possible, or should I just give up and put the image in the same directory as the .exe?

You need to go down one further level in order to get to the root.

../../resources/dot.bmp

您的可执行文件在bin/Debug中,但我认为您是在假设它在bin中的情况下进行编码的。

假设您在Windows上,相对路径将相对于当前工作目录,而不是可执行文件所在的目录。它们通常是相同的东西,但不一定。

我倾向于使用完全限定的路径,并预先挂起可执行文件所在的目录。您可以通过调用GetModuleFileName并将NULL作为hModule参数来获得此值。这将返回可执行文件的完整路径,因此您需要去掉文件名部分。

您还需要考虑部署。这个结构看起来像你的开发结构,但当你部署程序时,你可能想要一个不同的组织。例如,我希望可执行文件在部署时位于bin目录中。

最后一个想法。假设您的程序需要的映像在编译时是已知的,那么将它们作为资源链接到可执行文件中会容易得多。这样一来,您根本不必担心这些问题,并且可执行文件可以独立运行。