LWChris
LWChris

Reputation: 4201

"Cannot open source file" for header files of installed SDK

I am trying to continue to work on a C++ project that somebody else started. To open the project, I had to install the C/C++ workload; I did that and already restarted my PC.

The project originally targeted a different Windows SDK version than the one that is installed on my PC. This lead to Visual Studio IntelliSense telling me "Cannot open source file" for SDK header files like stdio.h or string.h for example.

I went to the project settings > VC++ Directories > Include Directories > Edit and saw that the variables expanded to the wrong paths. Thus I proceeded to retarget the project to the Windows SDK version that is currently installed on my PC (10.0.17763.0).

Now, if I check the Include Directories, I see that "Inherit from parent or project defaults" is checked, the inherited value is

$(VC_IncludePath)
$(WindowsSDK_IncludePath)

and that this expands to

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\atlmfc\include
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\VS\include
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\shared
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\winrt
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\cppwinrt
C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\Include\um

I checked that all of these directories do exist on my machine.

However, VS keeps telling me that it cannot open the source files, even after cleaning and rebuilding the whole solution. The project seems to compile, but the code is all squiggly.

My includes are:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <float.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <locale.h>
#include "svm.h"

Only stdarg.h, limits.h (both from C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\) and svm.h (in project) work. All other includes have this error; they all come from the directory C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt, I can find all of them in that directory.

I noticed that when I type #include < and let IntelliSense suggest includes, it also does not suggest those from any of the C:\Program Files (x86)\Windows Kits\10\ subdirectories. These header files are listed in the "External Dependencies" project node though.

StackOverflow is full of questions about this error, but usually about a third party lib or project file, and the answers suggest to check the Include Directories. I did that, and they appear to be complete if I can trust the expanded value, but apparently something is still wrong.

I am basically new to C++ and all the "header magic", so it might be related to the project. I found that the project contains some "stdafx.h" file (which is as I read for precompiled headers), however the project settings say to not use precompiled headers and indeed I cannot see any *.pch file either.

Upvotes: 4

Views: 7651

Answers (1)

LWChris
LWChris

Reputation: 4201

Encouraged by Ted Lyngmo's comment I started to edit one of the imports, when suddenly all errors disappeared and the problem was resolved, although nothing had changed.

I had done that before and it didn't help then, so unfortunately I don't know why it helped the second time. As unsatisfactory as it is - it seems that this was a caching issue after all that even persisted after clean and rebuild.

Maybe the difference was that the second time I edited was after the clean and rebuild of the solution, but I can't guarantee.

I'm not very happy with this outcome, because I would've liked to understand what helped and why, but in the end this solved the problem. So it seems you have to retarget, then clean, then rebuild, maybe restart VS, and edit some imports to force whichever cache there is to be invalidated.

P.S.: Also, in the mean time, go and participate in StackOverflow. Apparently this helps. Thus I'm marking this as community answer.

Upvotes: 1

Related Questions