无法打开包含文件:'stdio.h' - Visual Studio Community 2017 - C++ 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44219901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error
提问by dahiana
I am trying to Build a Solution on Visual Studio Community 2017, but I keep getting the error "Cannot open include file: 'stdio.h'". I've read through several similar questions, but still can't fix this problem. Looks like the stdio.hfile is called in the stdafx.hfile. Below are more details. Any suggestions? (I can't embed images yet, so please click on the links for screenshots.)
我正在尝试在 Visual Studio Community 2017 上构建解决方案,但我不断收到错误“无法打开包含文件:‘stdio.h’”。我已经阅读了几个类似的问题,但仍然无法解决这个问题。看起来在stdafx.h文件中调用了stdio.h文件。以下是更多详细信息。有什么建议?(我还不能嵌入图片,所以请点击链接查看截图。)
System details:Windows 10
Visual Studio Community 2017 v.15.2 (26430.6)
-- Installed Desktop Development with C++ (Screenshot: Installation list)
系统详细信息:Windows 10
Visual Studio Community 2017 v.15.2 (26430.6)
-- 使用 C++ 安装桌面开发(截图:安装列表)
Step 1:I wrote the famous Hello World program in C++.
第 1 步:我用 C++ 编写了著名的 Hello World 程序。
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Step 2:I clicked on Build > Build Solution.
第 2 步:我单击“构建”>“构建解决方案”。
Problem:'stdio.h': No such file or directory. Full Error:
问题:'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 ==========
Troubleshooting details/Things I've tried:
故障排除详细信息/我尝试过的事情:
- Configuration Properties > VC++ Directories
Include Directories $(VC_IncludePath);$(WindowsSDK_IncludePath);
- Screenshot: Solution Explorer (files in the project)
Code in stdafx.cppfile:
// 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
Code in stdafx.hfile:
// 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>
NOTE: the
#include
for<stdio.h>
and<tchar.h>
both have the red squiggle line underneath, and says "cannot open source file".
TRIED: I tried removing the last two lines, but then I got more errors.TRIED: Since many suggested that stdafx.h is not required, I tried removing just the first line,
#include "stdafx.h"
. But in order for this to work I had to do a little more. SEE ANSWER BELOW.
- 配置属性 > 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
for<stdio.h>
和<tchar.h>
两者下面都有红色波浪线,并表示“无法打开源文件”。
尝试:我尝试删除最后两行,但后来出现了更多错误。尝试:由于许多人建议不需要 stdafx.h,我尝试只删除第一行
#include "stdafx.h"
. 但为了让这个工作,我不得不做更多的事情。请参阅下面的答案。
采纳答案by dahiana
There are three ways to solve this issue.
有三种方法可以解决这个问题。
- Ignore Precompiled Headers #1
Steps:Project > Properties > Configuration Properties > C/C++ > Command Line > in the Additional Options box add /Y-. (Screenshot of Property Pages) > Ok > Remove#include "stdafx.h"
- Ignore Precompiled Headers #2
Steps:File > New > Project > ... > In the Application Wizard Window click Next > Uncheck the Precompiled Header box > Finish > Remove#include "stdafx.h"
Reinstall Visual Studio
This also worked for me, because I realized that maybe there was something wrong with my Windows SDK. I was using Windows 10, but with Windows SDK 8.1. You may have this problem as well.
Steps:Open Visual Studio Installer > Click on the three-lined Menu Bar > Uninstall > Restart your computer > Open Visual Studio Installer > Install what you want, but make sure you install only the latest Windows SDK 10, not multiple ones nor the 8.1.The first time I installed Visual Studio, I would get an error stating that I needed to install Windows SDK 8.1. So I did, through Visual Studio Installer's Modify option. Perhaps this was a problem because I was installed it after Visual Studio was already installed, or because I needed SDK 10 instead. Just to be safe I did a complete reinstall.
- 忽略预编译头文件 #1
步骤:项目 > 属性 > 配置属性 > C/C++ > 命令行 > 在附加选项框中添加 /Y-。(属性页的屏幕截图)> 确定 > 删除#include "stdafx.h"
- 忽略预编译头文件#2
步骤:文件 > 新建 > 项目 > ... > 在应用程序向导窗口中单击下一步 > 取消选中预编译头文件框 > 完成 > 删除#include "stdafx.h"
重新安装 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。为了安全起见,我进行了完整的重新安装。
回答by Maxim Akristiniy
Got same problem with project porting from VS2013 to VS2017,
Fix: change "Properties->General->Windows SDK Version" to 10
项目从 VS2013 移植到 VS2017 时遇到同样的问题,
修复:将“属性->常规->Windows SDK 版本”更改为 10
回答by ysap
Faced the problem of missing stdlib.h
and stdio.h
(and maybe more) after installing VS2017 Community on a new computer and migrating a solution from VS2013 to VS2017.
在新计算机上安装 VS2017 Community 并将解决方案从 VS2013 迁移到 VS2017 后,面临丢失stdlib.h
和stdio.h
(可能更多)的问题。
Used @Maxim Akristiniy's proposal, but still got error message regarding toolset compatibility. However VS itself suggested to do solution retarget by right-clicking on the solution in Solution Explorer, then selecting Retarget solution
from the menu and the updated Windows SDK Version
from the drop-down list.
使用了@Maxim Akristiniy的提议,但仍然收到有关工具集兼容性的错误消息。然而,VS 本身建议通过右键单击解决方案资源管理器中的解决方案,然后Retarget solution
从菜单中选择并Windows SDK Version
从下拉列表中更新来进行解决方案重定向。
Now my projects build w/o a problem.
现在我的项目构建没有问题。
Note that you may need to make the project your startup project for the retargeting to catch.
请注意,您可能需要将该项目设为您的启动项目,以便进行重新定位。
回答by Sapnesh Naik
For CUDA:
对于 CUDA:
Right Click on your project.
右键单击您的项目。
Go to Properties->CUDAand set "CUDA Toolkit Custom Dir" to your CUDA toolkit directory.
转到Properties->CUDA并将“ CUDA Toolkit Custom Dir”设置为您的 CUDA 工具包目录。
For me it was: C:\\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0
对我来说是:C:\\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0
回答by Hemaolle
Just in case you don't want to bump Windows SDK to Windows 10 (you could be for example working on an open source project where the decision isn't yours to make), you can solve this problem in a Windows SDK 8.1 project by navigating Tools -> Get Tools and Features... -> Individual Compontents tab
and installing the individual components "Windows 8.1 SDK" (under SDKs, libraries and frameworks) and "Windows Universal CRT SDK" (under Compilers, build tools and runtimes):
以防万一您不想将 Windows SDK 撞到 Windows 10(例如,您可能正在从事一个不由您做出决定的开源项目),您可以通过以下方式在 Windows SDK 8.1 项目中解决此问题导航Tools -> Get Tools and Features... -> Individual Compontents tab
和安装单个组件“Windows 8.1 SDK”(在 SDK、库和框架下)和“Windows Universal CRT SDK”(在编译器、构建工具和运行时下):
回答by CoffeDeveloper
Faced the same issue, another solution is to add default includes, this fixed the problem for me:
面对同样的问题,另一种解决方案是添加默认包含,这为我解决了问题:
$(IncludePath);
回答by Shazzer
If you run into this problem with Visual Studio 2019 (VS2019), you can download the build tools from https://visualstudio.microsoft.com/downloads/. And, under Tools for Visual Studio 2019 and download Build Tools for Visual Studios 2019.
如果您在使用Visual Studio 2019 (VS2019) 时遇到此问题,您可以从https://visualstudio.microsoft.com/downloads/下载构建工具。并且,在 Tools for Visual Studio 2019 下并下载Build Tools for Visual Studios 2019。
回答by Ted
Scenario:
设想:
Windows 10with Visual Studio 2017(FRESH installation).
'C' project(ERRORlike -> cannot open source file: 'stdio.h', 'windows.h', etc.).
带有Visual Studio 2017 的Windows 10(全新安装)。
“C”项目(错误如 ->无法打开源文件:“stdio.h”、“windows.h”等)。
Resolve:
解决:
Run 'Visual Studio Installer'.
Click button 'Modify'.
Select 'Desktop development with C++'.
From "Installation details"(usually on the right-sidebar) select:
4.1. Windows 10 SDK(10.0.17134.0).
- Version of SDK in 4.1. is just for example.
Click button 'Modify', to apply changes.
- Right-click 'SomeProject' -> 'Properties'.
- 'Configuration:' -> 'All Configurations' and 'Platform:' -> 'All Platforms'.
- 'Configuration Properties' -> 'General' -> 'Windows SDK Version':
- change(select from combobox) SDK version to currently installed;
- Click button 'Apply', to apply changes.
运行“ Visual Studio 安装程序”。
单击“修改”按钮。
选择“使用 C++ 进行桌面开发”。
从“安装详细信息”(通常在右侧栏)中选择:
4.1. Windows 10 SDK(10.0.17134.0)。
- 4.1 中的 SDK 版本。只是举例。
单击“修改”按钮以应用更改。
- 右键单击“SomeProject”->“属性”。
- “配置:”->“所有配置”和“平台:”->“所有平台”。
- “配置属性”->“常规”->“ Windows SDK 版本”:
- 将(从组合框中选择)SDK 版本更改为当前安装的版本;
- 单击“应用”按钮以应用更改。
回答by Andy Lucny
A dirty fix: Add $(VC_IncludePath);$(WindowsSDK_IncludePath); into project Properties / C/C++ / General / Additional include directories
一个肮脏的修复:添加 $(VC_IncludePath);$(WindowsSDK_IncludePath); 进入项目 Properties / C/C++ / General / Additional include 目录
回答by David Goeken
I had a similar problem after updating my VS2017. Project built fine; but lots of 'errors' when code was brought up in the editor. Even tried reinstalling VS. I was able to solve it by setting the option “Ignore Standard Include Paths” to Yes. Attempted to build the solution with lots of errors. Went back and set the option to No. After rebuilding, my problem went away.
更新我的 VS2017 后我遇到了类似的问题。项目建设良好;但是在编辑器中调出代码时会出现很多“错误”。甚至尝试重新安装VS。我能够通过将选项“忽略标准包含路径”设置为是来解决它。试图构建有很多错误的解决方案。返回并将选项设置为否。重建后,我的问题消失了。