程序可以在VS2013中运行,但不能在.exe中运行

Program works in VS 2013 but not the .exe

本文关键字:运行 但不能 exe VS2013 程序      更新时间:2023-10-16

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

我读到的关于这个问题的大多数答案都对应于从visualstudio内部加载.exe。我也尝试过释放模式,但同样的事情也发生了。

Sprite文件保存在项目文件夹中。VisualStudioIDE的默认运行位置是您正在执行的项目的项目文件夹。也就是说,它通常从保存.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."

自定义构建目标

将数据文件添加到项目中,并编写一个自定义生成脚本,该脚本执行相同的复制,但也建立一个输出依赖文件。

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).

嵌入式资源

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

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.

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

相关文章: