无法打开包含文件:'stdio.h' - Visual Studio Community 2017 - C 错误

新手上路,请多包涵

我正在尝试在 Visual Studio Community 2017 上构建解决方案,但我不断收到错误“ 无法打开包含文件:’stdio.h’ ”。

我已经阅读了几个类似的问题,但我仍然无法解决这个问题。看起来在 stdafx.h 文件中调用了 stdio.h 文件。以下是更多细节。有什么建议么?

系统详情:

  • 视窗 10
  • Visual Studio Community 2017 v.15.2 (26430.6) – 使用 C++ 安装桌面开发( 截图:安装列表

第 1 步: 我写了著名的 Hello, World! C++ 中的程序。

 #include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, World!" << endl;
    return 0;
}

第 2 步: 我点击了 BuildBuild Solution

问题: _‘stdio.h’ :没有这样的文件或目录_。完全错误:

 1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
1>stdafx.cpp
1>c:\users\dahiana mini\desktop\learncpp\helloworld\helloworld\stdafx.h(10):
    fatal error C1083: Cannot open include file: 'stdio.h': No such file or directory
1>Done building project "HelloWorld.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


故障排除细节和我尝试过的事情:

  1. 配置属性\* → VC++ 目录 Include Directories $(VC_IncludePath);$(WindowsSDK_IncludePath);

  2. 屏幕截图:解决方案资源管理器(项目中的文件)

  3. stdafx.cpp 文件中的代码:

    // stdafx.cpp : source file that includes just the standard includes
   // HelloWorld.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

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

   #include "targetver.h"
   #include <stdio.h>
   #include <tchar.h>

注意: #include 用于 <stdio.h><tchar.h> 下方都有红色波浪线,并显示“无法打开源文件”。

尝试过:我尝试删除最后两行,但后来出现更多错误。

  1. 尝试过:由于许多人建议不需要 stdafx.h,我尝试仅删除第一行 #include "stdafx.h" 。但为了让它发挥作用,我必须做更多的事情。 请参阅下面的答案。

原文由 dahiana 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 3.8k
2 个回答

有三种方法可以解决这个问题。

  1. 忽略预编译的头文件 #1

步骤: 项目 > 属性 > 配置属性 > C/C++ > 命令行 > 在附加选项框中添加 /Y-。 ( 属性页截图)>确定>删除 #include "stdafx.h" 2. 忽略预编译头 #2

步骤: 文件>新建>项目> …>在应用程序向导窗口中单击下一步>取消选中预编译头框>完成>删除 #include "stdafx.h" 3. 重新安装 Visual Studio

这也对我有用,因为我意识到我的 Windows SDK 可能有问题。我使用的是 Windows 10,但使用的是 Windows SDK 8.1。你也可能有这个问题。

步骤: 打开 Visual Studio 安装程序 > 单击三行菜单栏 > 卸载 > 重新启动计算机 > 打开 Visual Studio 安装程序 > 安装所需的内容,但请确保仅安装最新的 Windows SDK 10,而不是多个或 8.1 .

第一次安装 Visual Studio 时,我会收到一条错误消息,指出我需要安装 Windows SDK 8.1。所以我做到了,通过 Visual Studio 安装程序的修改选项。也许这是一个问题,因为我是在安装 Visual Studio 之后安装它,或者因为我需要 SDK 10。为了安全起见,我进行了完全重新安装。

原文由 dahiana 发布,翻译遵循 CC BY-SA 3.0 许可协议

对我来说,问题是我的 Windows 帐户没有权限。以下网址帮助我确定了问题:

Visual Studio 中的 stdio.h

Microsoft C++ 编译器,致命错误 C1083:无法打开包含文件“stdio.h”

https://social.msdn.microsoft.com/Forums/vstudio/en-US/5402a5ae-82e5-4370-9297-5be12152ce5e/standard-users-cannot-open-windowsh-using-x64-native-tools

最值得注意的是文本“如果您的标准用户想要使用本机工具编译 win32 项目,他们必须有权访问注册表编辑工具”。我在 Windows 中切换用户并使用管理员帐户登录,问题就消失了。

原文由 Jaco de Groot 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题