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.
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)