Visual Studio 2019 can't import header files

I think this may be a path issue but I can't account for it. In Visual Studio 2019 I'm trying to import source files for a C++ project but it keeps telling me it can't find it even though I'm looking right at it in the folder. This is my setup:

in my /src folder I have:
folder obj
folder logs
folder extras
folder configure
50 .cpp files
35 .h files

Now, in the VS 2019 Solution Explorer, I right click on Source Files/add.../existing items... navigate to the above /src folder, and select, manually, all the .cpp and .h files (excluding all the folders). Then, in the Solution Explorer, I move all the header files to the Header Files folder. This compiles as intended.

In order to save myself some time, because I do this 20 times a day during testing, in the /src folder I created a CPP folder and a Header folder. I put a copy of all header files in the Header folder and a copy of all the .cpp files in the CPP folder. These are copies so all the original files are still in the /src folder. So now:

in my /src folder I have:
folder obj
folder logs
folder Header (with copies of the 35 header files)
folder extras
folder CPP (with copies of the 50 .cpp files)
folder configure
50 .cpp files
35 .h files

Now, in the VS 2019 Solution Explorer, I right click on Source Files/add.../existing items... navigate to the above /src/CPP folder, ctrl+a (much quicker that selecting manually), and select Add. Then, in Solution Explorer, I right click on Header Files/add.../existing items... navigate to the above /src/Header folder, ctrl+a (much quicker that selecting manually), and select Add.

So I have the exact same setup as before: In Solution Explorer all the .cpp files are in the Source Files folder. All the header files are in the Header Files folder. In my /src folder all the files (header and .cpp) are in the same place. But when I try to compile I get a bunch of errors:

Severity Code Description Project File Line Suppression State
Error C1083 Cannot open include file: 'include.h': No such file or directory test735 C:\DS\TEST\src\CPP\wizard.cpp 20

saying that it can't find any of the header files. Doesn't make sense. In both scenarios the exact same files are in the exact same folders. Does it matter that it's looking for the files at C:\DS\TEST\src\CPP\ instead of C:\DS\TEST\src\? Either way it has the same files in both locations.

What am I missing?
Last edited on
In the Solutions Explorer pane select the Release (or Debug or whatever build you are playing with) and open the Properties... item.

Down in there you should see Include Directories and the like. Make sure that the directory for your include files is listed in the path.

I presume that adding the .cpp files to the project added their path properly, but you should check that as well.

If you are just testing existing projects, there should be an existing Project Solution (.sln) file that goes with the source code.


If you are doing daily testing with multiple different projects that require you to add a bunch of folders, AND you don't have .sln files or any other build script (which you should, btw), you have two basic options:

  • simply do the manual thing over and over (as it seems to be your job, so just do it?)
  • learn to use the command prompt to compile — you can easily write a script (using any
    scripting language you find convenient) to build yourself a simple script to do it. It can
    be as easy as a simple batch file:

1
2
3
4
5
6
7
8
9
@echo off
setlocal
:: cc.bat include-dir libs-dir source-dir exe-name
if not "%1"=="" set INCLUDE=%1;%INCLUDE%
if not "%2"=="" set LIBS=%2;%LIBS%
pushd %3
cl /EHsc /W4 /Ox ... *.cpp /Fe:%4
popd
echo done

Use it easily:

C:\Users\BishopOsiris\SomeProject> cc . "" . foo.exe

or

C:\Users\BishopOsiris\SomeProject> cc Header "" CPP foo.exe


Another option is to learn MSBuild (https://markheath.net/post/getting-started-with-msbuild), which takes a little getting used to but makes life very easy.

Another option is to frob CMake (https://cmake.org) to create NMake makefiles for your projects.

All of these options can be automated to some degree, or made simple enough that opening VS to play with everything directly can be avoided.


I really do NOT recommend duplicating files like you seem to suggest. This just introduces a failure point...

Hope this helps.
Topic archived. No new replies allowed.