2017-05-27 265 views
7

我想在Visual Studio Community 2017上构建一个解决方案,但我一直收到错误“无法打开包含文件:'stdio。 h'“。我已经阅读了几个类似的问题,但仍无法解决这个问题。看起来像stdio.h文件在stdafx.h文件中调用。以下是更多细节。有什么建议么? (我不能嵌入图像还,所以请点击截图的链接。)无法打开包含文件:'stdio.h' - Visual Studio Community 2017 - C++错误

系统细节: 的Windows 10
的Visual Studio 2017年社区v.15.2(26430.6)
- 安装的桌面开发用C++(Screenshot: Installation list


第1步:我写在C著名的Hello World程序++。

#include "stdafx.h" 
#include <iostream> 
using namespace std; 

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

步骤2:我点击生成>生成解决方案。

问题:'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. Screenshot: Solution Explorer (files in the project)
  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 
    
  4. 守则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>都有下方的红色波浪线线,并说“不能开源文件“。
    TRIED:我试图删除最后两行,但后来我得到更多的错误。

  5. TRIED:由于许多人提示stdafx.h不是必需的,我尝试删除第一行#include "stdafx.h"。但为了这个工作,我不得不多做一点。 请看下面的答案。

+2

那么,首先删除stdafx.h include,因为它在VS2017中是非标准和完整的*不必要*。 – DeiDei

+1

禁用预编译头并删除stdafx *。 – 2017-05-27 18:14:06

+0

@DeiDei哦我其实已经试过了。我会在上面添加它。在某种程度上这对我并不适用,因为我只删除了这行,'#include“stdafx.h”'。我不得不多做一点,这是我认为@ manni66的建议。首先,我按照[这里]的说明(https://docs.microsoft.com/en-us/cpp/build/reference/y-ignore-precompiled-header-options)将我的选项设置为忽略预编译头。然后,我删除了代码中的那一行。感谢大家的投入! – dahiana

回答

2

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

  1. 忽略预编译头#1
    步骤:项目>属性>配置属性> C/C++>命令行>在其他选项框添加/ Y-。 (Screenshot of Property Pages)> OK>删除#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 Installer的修改选项。也许这是一个问题,因为我已经在安装Visual Studio之后安装了它,或者因为我需要SDK 10。为了安全起见,我做了一个完整的重新安装。

+0

选项#1的问题是您必须为每个项目执行此操作。如果我找到更好的解决方案,我会在这里发布。 – dahiana

27

得到了与项目移植从VS2013到VS2017同样的问题,
修复:改变 “属性 - >常规 - > Windows SDK的版本”,以10

+2

这对我有效。谢谢!我还会补充说,可以通过在解决方案资源管理器中右键单击项目来获得这些属性。 – Vardit

+0

不知道为什么你的答案没有被选择为正确的,对我来说工作得很好。 –

5

面对缺少stdlib.hstdio.h的问题(也许更多)在新计算机上安装VS2017社区后,将解决方案从VS2013迁移到VS2017。

使用@Maxim Akristiniy的建议,但仍然收到有关工具集兼容性的错误消息。然而,VS本身建议通过右键点击解决方案资源管理器中的解决方案,然后从菜单中选择Retarget solution,并从下拉列表中选择更新的Windows SDK Version,从而完成解决方案重定向。

现在我的项目没有问题。

请注意,您可能需要将项目设为启动项目以重新捕获目标。

+0

我已经注意到(令人沮丧),有时候必须退出Visual Studio,并在任何这种摆弄之后重新启动它。 – HostileFork

相关问题