我正在尝试在 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 步: 我点击了 Build → Build 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 ==========
故障排除细节和我尝试过的事情:
配置属性\* → VC++ 目录
Include Directories $(VC_IncludePath);$(WindowsSDK_IncludePath);
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
- 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>
下方都有红色波浪线,并显示“无法打开源文件”。
尝试过:我尝试删除最后两行,但后来出现更多错误。
- 尝试过:由于许多人建议不需要 stdafx.h,我尝试仅删除第一行
#include "stdafx.h"
。但为了让它发挥作用,我必须做更多的事情。 请参阅下面的答案。
原文由 dahiana 发布,翻译遵循 CC BY-SA 4.0 许可协议
有三种方法可以解决这个问题。
步骤: 项目 > 属性 > 配置属性 > 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。为了安全起见,我进行了完全重新安装。