2017-05-27 562 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

相關問題