ERROR: Cannot open source file " "

Visual StudioVisual C++

Visual Studio Problem Overview


I am running visual studio C++ and I have a header file "GameEngine.h" that I am trying to have another file see.

When I #include "GameEngine.h" it gives me the error that it cannot open the source file. I have no idea what to do. I have done this literally a thousand times but for some reason this is now not working.

Visual Studio Solutions


Solution 1 - Visual Studio

You need to check your project settings, under C++, check include directories and make sure it points to where GameEngine.h resides, the other issue could be that GameEngine.h is not in your source file folder or in any include directory and resides in a different folder relative to your project folder. For instance you have 2 projects ProjectA and ProjectB, if you are including GameEngine.h in some source/header file in ProjectA then to include it properly, assuming that ProjectB is in the same parent folder do this:

include "../ProjectB/GameEngine.h"

This is if you have a structure like this:

> Root\ProjectA > > Root\ProjectB <- GameEngine.h actually lives here

Solution 2 - Visual Studio

Just in case there is someone out there who's a bit new like me, double check that you are spelling your header folders correctly.

For example:

<#include "Component/BoxComponent.h"

This will result in the error. Instead, it needs to be:

<#include "Components/BoxComponent.h"

Solution 3 - Visual Studio

One thing that caught me out and surprised me was, in an inherited project, the files it was referring to were referred to on a relative path outside of the project folder but yet existed in the project folder.

In solution explorer, single click each file with the error, bring up the Properties window (right-click, Properties), and ensure the "Relative Path" is just the file name (e.g. MyMissingFile.cpp) if it is in the project folder. In my case it was set to: ..\..\Some Other Folder\MyMissingFile.cpp.

Solution 4 - Visual Studio

This was the top result when googling "cannot open source file" so I figured I would share what my issue was since I had already included the correct path.

I'm not sure about other IDEs or compilers, but least for Visual Studio, make sure there isn't a space in your list of include directories. I had put a space between the ; of the last entry and the beginning of my new entry which then caused Visual Studio to disregard my inclusion.

Solution 5 - Visual Studio

For someone who's writing a CUDA program, you'll also need to need to configure the Additional Include Directories under the CUDA C/C++ tab under project properties.

Solution 6 - Visual Studio

Let Unreal do the job. Close all, Right click your Project File (.uproject),
"Generate VisualStudio Project Files".

Solution 7 - Visual Studio

  1. Copy the contents of the file,
  2. Create an .h file, give it the name of the original .h file
  3. Copy the contents of the original file to the newly created one
  4. Build it
  5. VOILA!!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNickView Question on Stackoverflow
Solution 1 - Visual StudioEdChumView Answer on Stackoverflow
Solution 2 - Visual StudioMegan FrazierView Answer on Stackoverflow
Solution 3 - Visual StudioMatt ArnoldView Answer on Stackoverflow
Solution 4 - Visual StudioQuickNET TechView Answer on Stackoverflow
Solution 5 - Visual StudiodontlooView Answer on Stackoverflow
Solution 6 - Visual StudioMartin ZrcekView Answer on Stackoverflow
Solution 7 - Visual StudioSolomon FissehayeView Answer on Stackoverflow