C++ classes in subfolders can't find their own header and won't compile

I’m trying to copy over some code from another project and I’m running into an issue where if the class is in a subfolder, when I try to build I get an error that it can’t find the include file. For example, if my project is (using a test project) NewCPP, and I created the class (from the Content Browser) named MyTestActor in a subfolder named Test…
The files will be here:
image

The default include line looks like this:
#include "Test/MyTestActor.h"
I get an error as such:
C:\Unreal Projects\NewCPP\Source\NewCPP\Test\MyTestActor.cpp(4): fatal error C1083: Cannot open include file: 'Test/MyTestActor.h': No such file or directory

However if I change my include to just be this:
#include "MyTestActor.h"
Then I can build successfully.

If I reference this class from a different class I do have to include the “Test/” part, but not from MyTestActor.cpp.

I can’t figure out why this is happening, there’s a good bit of code I’m copying over and it’s all organized neatly in folders, and it all works in two other projects without having to modify the include lines like this. I’m on a 5.2 project, but I’ve tried this with a 5.0 project, as both working projects are 5.0 projects now on 5.2, but it’s the same thing.

Hey @Fishmaster ,

I believe that is happening because both of the .cpp and .h of MyTestActor is in the same folder level. Yes it would be error if the include is Test/MyTestActor.h because it try to open Test folder where it already inside the folder itself (Test/Test/MyTestActor.h). That is why no need to put Test/ if it is in the same folder.

For other file, if you want to include the MyTestActor.h, you need to put Test/ if the file is in another level or maybe you need more specific path (Test/Etc/X.h). But if the file is in the same level, you won’t need Test/.

EDIT:
If you create a subfolder, this is expected to happen. EXCEPT if you separate the .h and .cpp with Public n Private folder, you won’t get this kind of error.

Thanks for the quick reply. The thing is, that’s how Unreal creates it, and that’s how all the files I’m looking to copy have their own include file. If say a file is in Source/Project/A/B/File.cpp and .h in the same folder, the cpp file will have "#include "A/B/File.h" in it. And in both other projects it works great, no underlines, no build errors.

yes, you can prevent that by separating the .cpp and .h I think. I have a project that separate the .h and .cpp, and everytime I create a new Cpp inside a subfolder, there is no error like this anymore. this is what I mean.

but as a side note, your auto generated cpp file might be separated outside the public folder like mine.
image

1 Like

So I found what it was. Inside the project’s .build file is apparently this line, placed there by one of the other programmers:
PrivateIncludePaths.Add(ModuleDirectory);

Adding this line allowed all the includes but also did two things:
First, it caused all these includes and much of the other code to go red underlined. This is fixed by adding this line as well:
PrivatePCHHeaderFile = "NewCPP.h"; (using my example project name)

Second, and more importantly, forced me to manually add a bunch of explicit engine includes like Engine.h, World.h, etc. for using things like GEngine, GetWorld(), and so on.

So I guess if anyone else comes across this, here’s the solution, take your pick :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.