C++ Why are files put in the Intermediate folder?

I see in my [GameName]Classes,h file is reads, do not modify, how to you add your header files to your .cpp files. Looking in ShooterGame, most ,ccp seems to point to ShooterGame.h but some of them follow the C++ convention I’m more used to and point to a header file of the same name.

How are new headers added to this list if you are not supposed to modify it?

Also, is it a bug that all new .h and .cpp files made in VS go into the Intermediate Folder? Should they be moved into a folder structure that mirrors the filter structure in the Solution Explorer?

1 Like

When you’re creating new C++ classes in Visual Studio, you’ll need to take note of where they’re placed. You definitely do not want to save them under the Intermediate folder. That will only lead to sadness. Here is the general idea:

  • If you plan you declare UObjects or UStructs in your header file (.h), you’ll need to make sure its saved under the Classes sub-folder for your game module. This is so that UnrealHeaderTool will be able to locate these headers, parse them, and automatically generate additional C++ code for those classes. For example:

    /Source//Classes/MyUObject.h

  • Most of your other non-UObject C++ header files (.h) should go in your module’s Private folder. This is just where we put source files and headers that aren’t exposed to other (sibling) modules.

    /Source//Private/MyGameFeature.h

  • All of your game’s C++ source files (.cpp) should go into your module’s Private folder. Source files are never “public”.

    /Source//Private/MyGameFeature.cpp

  • You may decide to use multiple C++ modules (.dll’s) for your game. If you do, then you can place classes in a Public folder to allow your game module’s sibling modules to access those APIs, or vice-versa. This is a more complicated use case that I can elaborate more on if you want. Also note that currently, all files in the “Classes” folder are also considered Public, even though most of the time they will only be used in the module that declared the class. An example of a publically exposed header would be:

    /Source//Public/MyPublicAPI.h

Let me know if you have any more questions like this!

–Mike

1 Like

Wonderful answer Mike, it helped clear up a lot of my confusion.

Thank you.

Hello Mike, could you elaborate on how to add multiple modules and how to share API’s?

It’s really easy, actually! For every *.Build.cs file that you create, UnrealBuildTool will automatically generate a DLL for that module. So you can create a new module by copying an existing Build.cs file and renaming/editing it appropriately, then adding source files into the new module’s Source directory.

Next time you generate project files, that module’s source will be discovered by UnrealBuildTool and added to your solution. Similarly, when you build your game project, as long as another module has a dependency on your new module, it will be built along with your game.

You create a dependency on your new module by modifying your game module’s Build.cs file. You can add your new module’s name to your game module’s PrivateDependencyModuleNames array. This will also automatically make the public header files in your new module available to be included in the game module. This is just one example of setting up a module dependency, there are other ways to do it – our build system is very flexible!

I should mention that we don’t really expect games to be built out of multiple modules very often. If you choose to do this it will be a more work for you and your team members. This is because programmers will need to create interfaces to access subsystems implemented in outside modules instead of just going straight to the code directly. You can argue the resulting code might be cleaner this way, but it may slow down your ability to iterate on code quickly.

Just for some context, our internal Fortnite game currently only has 3 game modules. However, we use dozens and dozens of interdependent modules for the game engine itself.

Hope this helps!

–Mike

1 Like

I made folders and moved the files into a windows structure that mirrored the CS filter structure.

I found that when building from VS, the new header class was added to the MyGameClasses.h

It looks like headers that go into the Classes folder get a generated header but headers put into the Private folder do not. Classes extending Slate stuff appear to go into the Private folder.

None of this explains why Visual Studio always has the intermediate folder as the default location when you go to add a new file (which is the source the OP’s confusion). What is so peculiar about the project files UE4 generates that VS does this?

2 Likes

Can you give me a video,i still confuse

1 Like

I’m still having this issue, 10 years later…

Even when I tell it to put a new file in source, it puts it in intermediate. Then I need to exit VS. Cut and paste the file to source, and Generate VS Project files. Then it works.

1 Like