程序适用于VS 2013,但不适用于.exe

Ste*_*her 3 c++ directx visual-studio-2013

我使用Direct X 11在Visual Studio 2013中制作了一个测试程序.它由一个简单的精灵组成,它根据计时器实现缓慢旋转.程序使用F5或Ctrl-F5加载并运行正常,但是当我尝试打开实际创建的.exe(在我的\ Debug文件夹中)时,它只显示窗口然后立即关闭.

我在这个问题上阅读的大多数答案都对应于从visual studio中加载.exe.我也试过发布模式,但同样的事情发生了.

Imr*_*zvi 6

精灵文件保存在项目文件夹中.Visual Studio IDE中的默认运行位置是您正在执行的项目的项目文件夹.也就是说,通常它是从保存.vcproj或.vcprojx文件的目录执行的(通常是解决方案目录文件夹下面的一个文件夹,保存.sln文件).

如果您的项目从IDE正确运行,但无法直接从调试文件夹运行,则很可能您依赖于项目文件夹中源项目旁边的项目数据文件.从Debug文件夹运行时,这些文件不再可见,因为Debug文件夹是您的工作目录; 不是项目文件夹.

有许多方法可以解决这个问题,每个方法都有自己的优点.一些选择是:

后期构建步骤

为项目创建一个后期构建步骤,将您的数据文件复制到项目的$(TargetDir)位置.然后,这些文件将显示在与可执行文件相同的目录中.

Benefit: Its easy.
Drawback: It will always run if you click "build solution" even if the data files are "up-to-date."
Run Code Online (Sandbox Code Playgroud)

自定义构建目标

将数据文件添加到项目中并编写执行相同副本的自定义构建脚本,但也会建立输出依赖项文件.

Benefit: Almost as easy as #1, but a little more tedious.
Drawback: You may have a lot of data files and each will require its own custom build step. (Note: you can multi-select all the data files in your project, and if you're creative with the built-in macros you can have them all use the "same" build rules and commands).
Run Code Online (Sandbox Code Playgroud)

嵌入式资源

将数据文件作为自定义资源添加到可执行文件中.

Benefit: Your project no longer requires data files side-by-side with the executable since they are embedded in the resource table of your EXE module.
Drawback: Custom code is required to dynamically load the custom resources from your executable's resource table rather than off-disk. It isn't difficult at all to do, but is additional work.
Run Code Online (Sandbox Code Playgroud)

还有其他选择,但我希望这会给你一些想法.

  • @Steve Hatcher您只需在"调试"下的项目设置中更改工作文件夹即可.通常你想把它改成`$(TargetDir)`(例如目标二进制文件夹).然后,在代码中,使用相对于二进制文件夹的文件路径(在您的情况下为`\ Debug`).例如:`../ resources/sprite.jpg`而不是`sprite.jpg`.(`..`在目录树中向上移动一级,因此`sprite.jpg`将始终在`resources`文件夹中从二进制文件夹向上一级搜索).做事情不是最简单和"正确"的方式吗?=)@Imran Rizvi你可以在你的答案中添加这个选项吗? (2认同)