首页 > 解决方案 > Visual Studio 2017 错误 - 找不到特定文件

问题描述

每次尝试运行代码时,我都会遇到同样的问题。“无法启动程序----系统找不到特定文件。我为以前版本的Visual Studio寻找了不同的解决方案,但它似乎不起作用。

这是出现的问题:

标签: c++visual-studio-2017

解决方案


问题是您的项目丢失了stdafx.h。此文件由 MSVS 自动生成。

  1. 最简单的解决方法——至少如果你对 MSVS 比较陌生——就是简单地创建一个新项目,然后从原始的“损坏”项目中复制/粘贴你的源代码。

  2. 另一种解决方案是禁用“预编译头”:

    https://msdn.microsoft.com/en-us/library/d7fz9ckx.aspx

    如果在项目中不使用预编译头文件,请将源文件的 Create/Use Precompiled Header 属性设置为 Not Using Precompiled Headers。要设置此编译器选项,请执行以下步骤:

    1. 在项目的“解决方案资源管理器”窗格中,右键单击项目名称,然后单击“属性”。

    2. 在左窗格中,单击 C/C++ 文件夹。

    3. 单击预编译头节点。

    4. 在右窗格中,单击“创建/使用预编译头”,然后单击“不使用预编译头”。

    注意:如果您选择禁用预编译头文件,请务必#include "stdafx.h"从所有源文件中删除。

  3. 有关更多信息,请查看此处:


附录:

  1. 在 Visual Studio 中创建一个新的 C++ 项目应该创建一个新的 stdafx.h。

  2. 或者,“设置项目 > 属性 > 预编译头文件 > 不使用预编译头文件”,然后#include stdafx.h从 .cpp 文件中删除应该可以修复启动错误。

...但是还有第三种方法...

  1. 您可以手动创建自己的 stdafx.* 文件并将它们添加到项目中。

    例子:

stdafx.h:

// stdafx.h : include file for standard system include files, or project 
// specific include files that are used frequently, but are changed infrequently
#pragma once

// TODO: reference additional headers your program requires here

stdafx.cpp:

// stdafx.cpp : source file that includes just the standard includes 
// hello_cpp.pch will be the pre-compiled header 
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H // and not in this file

targetver.h:

#pragma once    
// Including SDKDDKVer.h defines the highest available Windows platform.

// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.

#include <SDKDDKVer.h>

推荐阅读