68

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 I still can't fix this problem. It looks like the stdio.h file is called in the stdafx.h file. Below are more details. Any suggestions?

System details:


Step 1: I wrote the famous Hello, World! program in C++.

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

using namespace std;

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

Step 2: I clicked on BuildBuild Solution.

Problem: 'stdio.h': No such file or directory. Full Error:

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 and things I've tried:

  1. Configuration Properties* → VC++ Directories Include Directories $(VC_IncludePath);$(WindowsSDK_IncludePath);

  2. Screenshot: Solution Explorer (files in the project)

  3. Code in stdafx.cpp file:

    // 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. Code in stdafx.h file:

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

  5. 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 the answer below.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dahiana
  • 1,171
  • 2
  • 9
  • 11
  • 3
    Well, start by removing the stdafx.h include since it is non-standard and complete *unnecessary* in VS2017. – DeiDei May 27 '17 at 17:43
  • 2
    Disable precompiled header and remove stdafx*. –  May 27 '17 at 18:14
  • @DeiDei Oh I actually tried that. I'll add it above. In a way this did not work for me, because I only removed that line, `#include "stdafx.h"`. I had to do a little more, which is what I think @manni66 was suggesting. First, I set my options to ignore the precompiled headers by following the instructions [here](https://learn.microsoft.com/en-us/cpp/build/reference/y-ignore-precompiled-header-options). Then, I removed that line in the code. Thanks everyone for their input! – dahiana May 27 '17 at 20:12
  • 1
    Either way this is actually a bit weird, as must exist and work in any correctly configured compiler, even in C++ mode. – Paul Stelian May 27 '17 at 20:19
  • You may want to consider `#include `. – Thomas Matthews May 27 '17 at 20:40
  • 1
    @PaulStelian if that's the case, perhaps my error was mostly caused by my Windows SDK install not working well with my Visual Studio install. See answer below. – dahiana May 27 '17 at 20:47

15 Answers15

115

I got same problem with a project porting from Visual Studio 2013 to Visual Studio 2017.

Fix: change PropertiesGeneralWindows SDK Version to 10

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maxim Akristiniy
  • 2,121
  • 2
  • 15
  • 20
  • 9
    That worked fo me. Thanks! I would also add that these properties can be reached by right clicking on the project in the solution explorer. – Vardit Jul 18 '17 at 04:26
  • 2
    I also had to install the "Windows Universal CRT SDK" Visual Studio component as described here - https://stackoverflow.com/a/42778079/3893068 – Chris Koester Oct 01 '18 at 13:48
  • 1
    I had the same problem after changing nothing at all in the project. I would assume maybe something to do with windows updates? The version that was selected was 10.something but there was a never one available and that one worked. I assume that this new one used to not be there back when it worked. – Preza8 Oct 19 '18 at 10:06
  • On Microsoft Visual Studio Community 2017 15.7.1 I don't have "Windows SDK Version". However, right-clicking on solution and doing "Retarget solution" worked. – Swiss Frank Dec 15 '18 at 18:44
  • 1
    LOL, return to `` save changes, and then return again to `version 10` worked for me – Phi Feb 08 '19 at 17:04
  • I was having just a intellisense error. Changed the Win SDK version and it worked, changed it back to the lasted version and still works. Before coding in CUDA you need to debug the coding platform... – Pedro77 Jun 02 '19 at 19:50
21

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.

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.

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.

ysap
  • 7,723
  • 7
  • 59
  • 122
  • 1
    I've noticed (depressingly) that sometimes one has to exit Visual Studio and restart it after any of this fiddling. – HostileFork says dont trust SE Feb 07 '18 at 10:45
  • 1
    On Microsoft Visual Studio Community 2017 15.7.1 I don't have "Windows SDK Version" as mentioned in the top-vote-getting answer. However, your answer worked fine and I didn't have to restart anything. – Swiss Frank Dec 15 '18 at 18:45
12

There are three ways to solve this issue.

  1. 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"
  2. 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"
  3. 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.

dahiana
  • 1,171
  • 2
  • 9
  • 11
  • The problem with Option #1 is that you have to do this for every project. If I find a better solution, I will post it here. – dahiana May 27 '17 at 20:58
8

For CUDA:

Right Click on your project.

Go to Properties->CUDA and set "CUDA Toolkit Custom Dir" to your CUDA toolkit directory.

For me it was: C:\\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0

enter image description here

Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98
7

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):

Hemaolle
  • 1,950
  • 18
  • 21
  • 4
    I had to install the "Windows Universal CRT SDK" Visual Studio component in addition to changing the Windows SDK Version to 10. – Chris Koester Oct 01 '18 at 13:51
4

Scenario:

  1. Windows 10 with Visual Studio 2017 (FRESH installation).

  2. 'C' project (ERROR like -> cannot open source file: 'stdio.h', 'windows.h', etc.).

Resolve:

  1. Run 'Visual Studio Installer'.

  2. Click button 'Modify'.

  3. Select 'Desktop development with C++'.

  4. 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.
  5. Click button 'Modify', to apply changes.

  6. Right-click 'SomeProject' -> 'Properties'.
  7. 'Configuration:' -> 'All Configurations' and 'Platform:' -> 'All Platforms'.
  8. 'Configuration Properties' -> 'General' -> 'Windows SDK Version':
    • change(select from combobox) SDK version to currently installed;
  9. Click button 'Apply', to apply changes.
Ted
  • 843
  • 10
  • 13
3

I faced the same issue. Another solution is to add default includes.

This fixed the problem for me:

$(IncludePath);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
3

A dirty fix: Add $(VC_IncludePath);$(WindowsSDK_IncludePath); into project Properties / C/C++ / General / Additional include directories

Andy Lucny
  • 41
  • 5
3

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.

Shazzer
  • 31
  • 1
2

For me the problem was a lack of rights on my Windows account. The following url's helped me to identify the problem:

stdio.h in Visual Studio

Microsoft C++ Compiler, Fatal Error C1083: Cannot open include file '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

Most notably the text "your standard users must have access to registry editing tools if they want to compile win32 projects using Native Tools". I switched user in Windows an logged in with an admin account and the problem was gone.

1

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.

1

It was the same issue I was getting.

  • Run 'Visual Studio Installer' → ModifyWindows 10 SDK (10.0.17134.0).

  • Wait for the update to finish.

  • Restart your computer.

  • Open Visual Studio 2017 and open your project.

  • Right click on the project's Solution Explorer.

  • Select "Retarget Solution" and then select "Windows 10 SDK(10.0.17134.0)".

It worked for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ami
  • 233
  • 2
  • 9
0

Similar problem for me but a little different. I can compile and run the default CUDA 10 code with no problem, but there are a lots of error related to the stdio.h file show in the edit window. Which is annoying. I solve it by change the code file name from "kernel.cu" to "kernel.cpp". That is wired but works for me. And it runs well so far.

rml
  • 803
  • 10
  • 14
0

I got this kind of error, too, while trying to open a .cpp file with Vscode. The error persisted even after I re-installed my Vscode like someone suggested above. What worked for me was: In your Vscode, after opening the folder that contains your .cpp files, open command palette (ctrl+shift+p), and search for c/c++: "Edit configurations (UI)." Select it. Scroll down to "compiler path" and paste the path to your gcc compiler (for me, it was: C:\Program Files\CodeBlocks\MinGW\bin\gcc.exe). Note: I installed my gcc compiler through installing CodeBocks IDE.

That's all!

-1

I had the same problem building a Visual Studio 2013 project with the Visual Studio 2017 IDE.

The solution was to set the right "Platformtoolset v120 (Visual Studio 2013). Therefore the Windows SDK 8.1 installed must be installed. If you want to use Platformtoolset v141 (Visual Studio 2017) there must be Windows SDK 10.

The Platformtoolset can be chosen inside the properties dialog of the project: General * → Platformtoolset

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kombinator
  • 107
  • 6