c++ - 程序在 VS 2013 中有效,但在 .exe 中无效

标签 c++ directx visual-studio-2013

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

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

最佳答案

Sprite 文件保存在您的项目文件夹中。 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."

自定义构建目标

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

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.

还有其他选项,但我希望这能给您一些开始的想法。

关于c++ - 程序在 VS 2013 中有效,但在 .exe 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20042634/

相关文章:

c++ - DirectX DXUT 备用 API

utf-8 - Visual Studio Express 2013 for Web - 保存编码不变的文件

c# - 有什么方法可以检测外部进程是否在独占全屏模式下运行?

c++ - move 语义、标准集合和构造时间地址

c++ - 在 MFC 应用程序中定义和读取用户定义的资源

visual-studio - 在 Visual Studio 2013 上启用 SSL 不起作用

C++ 如何在 directx 中制作 GUI?

c++ - 挂其他问题?

c++ - 如何在 C++ 中返回本地数组?

c++ - 从c++ vector 中的指定索引中提取元素