The .generated.h file should always be the last #include in a header. Why is that?

If I try to include anything after the .generated file, I get this error:

#include found after .generated.h file - the .generated.h file should always be the last #include in a header

Obviously, I can live with including that file as a last include, but I’m wondering why Unreal has this requirement.

This is what ChatGPT is telling about .generated.h files, I couldn’t explain it any better:

"In Unreal Engine, generated.h files are auto-generated headers that play a crucial role in the Unreal Engine build process, especially in the context of Unreal’s reflection system and the Unreal Header Tool (UHT). "

The reflection system allows the engine to gather information about objects and their properties at runtime. I might be wrong, but I guess that anything that is included after this header would not be tracked by the reflection system.

Thanks but I can also ask ChatGPT. I chose to post here, because as you can see, this is not really an answer to my question… But thanks for trying.

1 Like

Basically the .generated files are what the engine uses during compile time. (they are created in the intermediate folder)
Your header files are mostly for human readable purposes. They export to the .generated which is what the engine actually uses.

The exported generated header files are more optimized than human readable versions. They also have extra declares and macros and serialization data.

As for why it should be last I’m guessing it’s contents can’t really be used directly by any header before it (there would be mismatches in naming conventions of functions). To keep this sanitized it’s relegated to the last include.

.generated probably also hides stuff like internal redirect information. Giving it a layer of abstraction.

They also help with interaction with blueprints (all blueprint macros create extended information inside of .generated files)

Hello,
you’ve to use your include bottom of your include lines. For example my EOSGameInstance.h is the generated file:

image