When I create a new C++ Class in a Subfolder

Hi,

I am very to C++ and Unreal and I searched for several days for an answer, but couldn’t find any so my apology if there was one somewhere.

Ok, when I create a new class. In my case I want create an
Actor Class
with the name:
Flyer (Flyer.cpp + Flyer.h)

in a subfolder called:
Dynamics

Then the Unreal generates a Flyer.cpp with the following include file:
#include “Dynamics/Flyer.h”
which of course fails to compile, since both files the cpp and the header file were generated in the same path. Of course I can manually fix this problem, but I can’t believe that this is the normal case for everyone else. Have I missconfigured something somewhere that I constantly get the wrong #includes?

1 Like

What does your general source directory look like?

All my source files are in subfolders and it works just fine. You’d want to look at your project’s build.cs file and make sure that the PrivateIncludePaths reference the right one(s). You’re probably missing the project path or the project’s public or private path.

The includes will work without the build.cs settings since, if you aren’t using subfolders, includes without paths are always allowed to reference files in the same directory as the file doing the include.

2 Likes

Hi,

Unfortunately I don’t know anything about these things. What I did was:

  1. Create New Project: “HelloWorld” and Unreal (4.26.2) created a Project folder for me:
    \Engine
    \Games
    \Games\HelloWorld
    \Games\HelloWorld\Source
    \Games\HelloWorld\Source\HelloWorld

  2. Then in the Unreal Editor I went to:
    \C++ Classes\HelloWorld\

  3. Right clicked and selected: New C++ Class

  4. I selected: Actor and clicked Next

  5. I chose: Flyer as Name
    and
    chose: E:/Unreal Projects/HelloWorld/Source/HelloWorld/Dynamics/ as Path

  6. I hit Create Class

Here I ended up with an error message and some hints that I have to re-compile the project before I would be able to see them in the Unreal Editor.

Back in Visual Studio, I now have the following created files:
\Games\HelloWorld\Source\HelloWorld\Dynamics\Flyer.cpp
\Games\HelloWorld\Source\HelloWorld\Dynamics\Flyer.h

when I try to build HelloWorld I end up that Visual Studio can’t find “Dynamics/Flyer.h” which is a result of the bad include command inside of Flyer.cpp (#include “Dynamic/Flyer.h”)

The Idea behind this actor was to work toward an object that would be visible in the world and eventually move it around.

Hope this explain better what I did.

I don’t actually know if you can fix this. Unreal Engine and Visual Studio do not play well together, and it’s really annoying.

I’d recommend using Rider for Unreal. It’s free right now, probably will cost money in the future.
You’ll still encounter this problem every now and then, but Rider will fix it automatically on your next compile.

1 Like

If you’re in Visual Studio, you should be able to find a file named “HelloWorld.Build.cs” either through the Solution Explorer window or however you normally jump to other files. It’s going to be a bunch of C# but don’t worry if you’ve never messed with that language before.

The top of the file will look something like this:

public class HelloWorld : ModuleRules
{
	public HelloWorld(ReadOnlyTargetRules Target) : base(Target)
	{

Inside that constructor, you’ll want to add (or modify the existing code) so that you have

PrivateIncludePaths.AddRange(
   new string[] {
      "HelloWorld",
      // ... add other private include paths required here ...
     }
);

within that function.

This should cause Unreal to execute compiles such that your includes can be whatever comes after …\Source\HelloWorld.

Why it didn’t do this by default, I’m not sure. I think it gets sort of confused when making modules that don’t have Public/Private folders. (In fact the PrivateIncludePaths may already include “HelloWorld/Private”, which doesn’t really do you any good).

2 Likes

I always get the same issue and I simply delete the name of the folder from the include. I’m not sure why this happens, but don’t let it bother you. Just fix the include to be:

include “Flyer.h”

2 Likes

I will look into this cs file and I will also try to understand the Private/Public folder thing and for what they’re good for. Thanks a lot for the help!

I had the same issue when starting w Unreal and took me a while to figure out. Some new projects seem to not set up the private and public folders properly for Unreal to use with C++. You want to make sure you are adding your new class to the Public folder for the proper setup and compile… ie:
/C++ Classes/projectname/Public/WhateverFolder/AnotherFolder/

Download the content examples or another project and look at the structure of the folders there to see how it should be set up. I’m not sure why it doesn’t set up properly, but I manually changed mine to the proper structure then re-built the .sln file and things worked fine afterwards.

2 Likes

@MrBadDay It’s good to know, but ultimately the public and private folders won’t really come into play until your working with multiple modules or writing you own plugins (not necessarily for sharing but personal organization). My game module doesn’t have either of them for example, but all my plugins do.

@Chip_Burwell That solution works great with the default behavior of includes that I described earlier but does fall apart once you have subfoldered code that you’re trying to access from another subfolder.

3 Likes

I have the same problem, the solution is that you just have to delete the folder name added in the #include of the .h or .cpp files, or both. It’s a bit annoying and I hope Epic Games will fix this issue in the near future!

3 Likes

Yes, annoying is an understatement. Unless you’re constantly developing something for UE, and know all the little obstacles by heart, these things are really time-consuming. Little custom nodes that would cost like 1-2 hours of coding turn into 3+ hours of constantly asking yourself why UE is doing this or that… and how to get around it.

While I’m not a friend of a certain other Game engine, coding there is definitely much easier.

1 Like

Delete these file / folders and rebuild the project.

HelloWorld.sln
.vs
Binaries
DerivedDataCache
Intermediate
Saved

Now right click the project explorer with U icon and select generate project files.
HelloWorld.sln will be re-generated, open it and set the project as startup defaults rebuild and thats all.

2 Likes

When creating the class, select either Public or Private.

… and that’s it.

// .cpp
#include "Misc/MiscInsideMisc/Text2ActorComponent.h" // This compiles

Also this:

2 Likes

Ok, this also works, thanks!

I just stumbled over this problem as well (with UE5).

My project doesn’t have the Private/Public subfolders for the main project (it does however for all plugins). All main files and folders are directly below the Source/<projectname> folder.

Adding the project name to PublicDependencyModuleNames in <projectname>.Build.cs didn’t help; the UE build system reported an error about it:

Invalidating makefile for <projectname>Editor (<projectname>.Build.cs modified)
Circular dependency on <projectname>.Build.cs detected.
Full Route: Target → <projectname>.Build.cs<projectname>.Build.cs
Cycled Route: is <projectname><projectname>.
Break this loop by moving dependencies into a separate module or using Private/PublicIncludePathModuleNames to reference declarations

Adding the project name to the PrivateIncludePaths did.

Sidenote: I don’t understand why the main project needs the distinction between private an public anyway because as a executable binary it doesn’t need to export symbols at all?