Visual Studio 2022: Cannot open include file: 'ctype.h'
Asked Answered
G

2

6

I've got the infamous error message in C++ build: "Cannot open include file: 'ctype.h'". I know a similar question was already asked multiple times, but my case seems different because I am using the latest Visual Studio 2022 and seems to behave differently.

I have a bunch of existing C++ projects, they use plain 32-bit Windows API with DirectX 6, and I used VC++ build tools 2015 and Windows SDK 8.1 to compile it without any issues in previous versions of Visual Studio. Everything was fine in Visual Studio 2019, no special setup was needed, until I uninstalled it and installed Visual Studio 2022.

Now I can open my solutions, it nicely shows they are using C++ build tools 2015, which I installed together with VS2022, and the solutions also shows correctly that they use Windows 8.1 SDK. But Windows 8.1 SDK is not present in VS2022 installation, I installed it separately. I also tried to "repair" VS2022 installation, but that only deleted all my UI preferences, but not fixed anything in build. Also, I tried to reinstall Windows 8.1 SDK, but it said it is already OK.

enter image description here

When looking to Visual Studio, I can't see any place where I can set default include and lib directories, I can only list what is being used right now. VS2022 shows this list:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\atlmfc\include
C:\Program Files\Windows Kits\10\Include\10.0.10240.0\ucrt
C:\Program Files (x86)\Windows Kits\8.1\Include\um
C:\Program Files (x86)\Windows Kits\8.1\Include\shared
C:\Program Files (x86)\Windows Kits\8.1\Include\winrt

The actual error message I receive is this:

c:\program files (x86)\windows kits\8.1\include\um\winnt.h(31): fatal error C1083: Cannot open include file: 'ctype.h': No such file or directory

So it says that winnt.h from windows kits 8.1 cannot find ctype.h. And yes, there is no ctype.h in that folder or anywhere around, I can see it only in C:\Program Files\Microsoft Visual Studio\2022\Professional\SDK\ScopeCppSDK\vc15\SDK\include\ucrt which I think is the folder with VC++ 2015 build tools. So this seems correct, but I am wondering why this folder is not a part of default include directories when VC++ 2015 build tools are set in project settings. Because it seems that VS2022 is correctly picking my Windows 8.1 SDK, but not picking correctly the older C++ compiler.

When I try to add the folder where ctype.h resides to include folders, I receive another type of errors saying that other files are incompatible with these include files. Of course, this system of directories needs to be in sync. So please what is the correct way of using this?

Also, I tried to switch the VC++ build tools to 2022 version. Unfortunately, that also does not fix the issue. And Windows 10 SDK is not installed, the software wants to stay compatible with older Windows, so I don't need it.

Gloss answered 8/2, 2022 at 14:17 Comment(0)
G
3

I found a bug report which is related: https://developercommunity.visualstudio.com/t/windows-81-sdk-1/151682

Although Microsoft staff declined to accept it as a bug, it is happening for me as well. When I install only Windows 8.1 SDK, no project can be compiled with it. I even tried to create a new Windows API project in VS2022, the project was created, but failed to compile with the same error.

So I tried to install Windows 10 SDK (from VS2022 installed, but that is probably not important) and this added some missing files which now help to compile Windows 8.1 SDK projects. Problem seems to be solved, my old C++ code can now be compiled with Windows 8.1 SDK and both C++ build tools 2015 and 2022.

Gloss answered 8/2, 2022 at 16:13 Comment(1)
In my case installing the Windows 8.1 SDK and going to Visual Studio Installer to install Windows 10 SDK was not enough. I tried installing a bunch of stuff with the VS Installer but the problem was finally fixed when I installed Windows Universal CRT SDK from "Individual components". This added the folder "C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt" which is referenced in the project's "VC++ Directories/Include Directories"Hitandmiss
K
2

In my case with 10.0.19041 the ucrt headers were installed under C:\Program Files (x86)\Windows Kits\10\ but builds were searching C:\Program Files\Windows Kits\10\. No amount of adding components, repairing, or reinstalling helped. Possibly it's related to having old versions of VS on the machine.

It's described as an SDK installer bug in https://developercommunity.visualstudio.com/t/incorrect-ucrt-path-in-vc-includepath-vs-2022/1678876 where they suggest hacks of

but the first already looked correct, the second didn't help, and the third made me fear masking rather than fixing the issue. Ultimately what actually helped was finding the "KitsRoot10" key in HKLM\SOFTWARE\Microsoft\Windows Kits\Installed Roots and HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots and making sure both had the right path, and restarting Visual Studio. It was immediately fixed, but I did a trial of uninstalling and reinstalling the SDK one last time just to make sure things remained stable.

'Diagnostic' level verbose build output pointed me at the broken IncludePath env var and the file Microsoft.Cpp.WindowsSDK.props it came from. Picking through the props file is how I found the registry key. And after everything was done, I found this same key problem in https://stackoverflow.com/a/70678191!

Knar answered 24/2, 2023 at 19:38 Comment(1)
This is still an issue in 2024, I found the same solution on UE forum from 2016..Thorley

© 2022 - 2024 — McMap. All rights reserved.