C++ project file structure: how to organize files in subfolders?

My directory structure of my unreal C++ project looks like this:

As the project grows, I would like to order my files. Like I can rearrange assets in the unreal content browser, I would like to group my C++ files.

Now the Jetbrains Rider refactor option to move a file into a subfolder isn’t available. This makes me hesitate. Do I actually have to move my files around manually? Somehow, the compiler knows where to look for all the C++ header files, too - In my new order, I would like to have the header files right next the .cpp files … but how to achieve that?

If I start a new unreal C++ project from scratch, there’s a different subfolder organization right from the start. If not with Rider, is there a tool maybe to migrate from one folder organization to another?

What is the recommended way to structure a big C++ project, anyway? All the header files in one place may have its merits when developing a library … but what about the usual unreal project?

I would split it up into

Game (instance, state, modes)
Characters
Systems (inventory, stats etc)
Ui (widgets)
Items (power ups, consumables)
Audio (sfx, music)
Library (static libs)

And inside if you have structs, enums or interfaces then put them in their own subdirectories

It changed depending on the type of project.
It’s also rather subjective.

3 Likes

thanks, do you have an idea how to shuffle the c files around, though?

How do I make sure that all the header and cpp files will be found still, after I changed the directory structure?

1 Like

I put all of my files in subdirectories and then when grabbing the headers from within cpp files would put in the directory

For instance inside my CharacterEx.cpp file i would import
#include “Item/Interfaces/PickupInterface.h”

it would be a header file in
public/Item/Interfaces/

Not a 100% sure if this is the best way but it works. I’m guessing the community will chime in if there is a better way

Just be sure to rebuild your project once you shuffle the files around.

Shut down project delete

  • .vs
  • Binaries
    -Intermediate
    -DerivedDataCache
  • saved
    .sln file

Right click you unreal engine project file and Generate Visual studio project.
Not sure if Rider uses the visual studio project file as a starting point or if it has another generation method (do you have a different prompt in the popup or can Rider read .sln files too?)

2 Likes

I will try this

I can confirm that I can shuffle files around quite liberally and from within Rider, there is a button “Refresh Solution” to reload everything.

I now move from a flat folder structure - in which private and public folders only contain files - to a nested structure, where the folder structure in public is mirrored within the private folder.

Of course, I have to change include directives accordingly, i.e. adding the new subfolders verbatim.

What I still don’t get:

As far as I understand, there isn’t any advantage in the private/public division. Am I right? And I can’t just delete private and public folder and shift all my folders on that level. My C++ projects expects header files in “public/”, and not in “/”. Where can I change that?

Public and Private are there more for keeping stuff hermetic. You only want to expose base information about headers, not their internal workings in the form of cpp files.

When including header files later on do not include the public part of the folder only what comes after it so for instance if I want to include my object pooler header file which is in public/pool/MasterPooler.h
I would only type in
#include “Pool/MasterPooler.h”

No need to add the prefix public folder.

Yes, I understand that there is this idea of keeping the public from the private.

However, I am not developping a library. So apart from noise, will there ever be a tangible benefit from mirroring the same folder structure in /public and /private? To me, it’s just extrawork w/o benefit. I might be mistaken, though.

And yes, too, I don’t prefix with “public/”.

EDIT: Rather, the public-privates folder structure seems to be implied by unreal (as it seems to be best practice for C++ projects). But the fact that any C++ unreal project created from scratch doesn’t have this fold structure made me think.

Well it’s the structure suggested by epic games. You can have it bunched up in a folder but I just use ctrl k → ctrl o shortcut in vs to jump between the header and it’s relevant cpp file. Does Rider implement a similar function?
This way it keeps the folder structure smaller, but that is personal preference I guess.

I use g d from Rider’s vim mode to jump between .cpp and .h. So thanks to the IDE, there’s isn’t a lot of trouble with this structure, once it is set up properly.

If you want all your .h and .cpp files in the same folder, in rider you can set file type nesting settings. That way the .h displays as the parent of the .cpp.

2 Likes