Cannot open source file "generated.h"

I can’t include X.generated.h header file in my project. For some reason when I try to include the header file I get "Cannot open source file “X_generated.h”. I’ve tired to generate Visual Studio project files, but didn’t help. I’ve found this question https://answers.unrealengine.com/questions/740950/cannot-open-source-file-namegeneratedh.html about the same issue, but still couldn’t resolve the problem. Any help would be greatly appreciated.

1 Like

Is it that last think you include in your header?

Yes, it is the last thing.

Try to clean your project.

  1. Close UE and VS
  2. Delete “.vs”, “Binaries”, “Intermediate” and “Saved” folders in your project root folder
  3. Double click your “.uproject” file and let the engine build everything.
  4. Generate VS files.
  5. Close the engine
  6. Open VS and recompile again
2 Likes

I’ve tried those steps before and didn’t resolve the error, but I’ll try again and let you know.

I tried your steps.

  • First thing is that when I double click .uproject, it doesn’t build everything and asks me to build the project manually. So I tired to build the project manually but it still says "Cannot open source file “X_generated.h”.

  • I’ve tested the header file with a project that already has the generated.h header file in it and it works without any problems, but whenever I try to add the generated.h file myself to my c++ code, it says "Cannot open source file “X_generated.h”.

Not sure what is causing this issue. I think a possible workaround would be start again, removing the .h and .cpp files completelly from your project and source folder, you will probably have to comment every reference to this files and its classes through the project. Right click the .uproject file and generate Visual Studio project files.

Then, you should compile with VS, start the engine editor and recreate the file with “File > New C++ class”. Paste the content back in the new files and recompile again. (using the autogenerated #includes)

Thank you all for you answers. I got the generated.h to work but its a bit buggy. By that I mean UE4 or VS sometimes works and sometimes doesn’t. I created the .cpp and.h file for five times and got it finally to work.

I know this is old but:

  1. This solution never worked for me;
  2. I actually managed to understand what is happening on my side.
  • If you find this happening again it could be a dependency issue related to Unreal modules.

  • Try to see in the compiler log the module it’s checking before the line of the error, it should point you to the module lacking the dependency.

  • The ‘.generated.h’ file belongs to the module you have to add (in the build.cs file).

Unreal documentation does a pretty good job for specifying the modules of most classes so you can check there which module you have to add.

Keep in mind that public dependency modules can only be referenced in the ‘public’ folders, meanwhile private dependency modules can only be referenced in the private folder.

1 Like

Small correction: I’m pretty certain you can use ‘public’ dependencies in both the ‘Public’ and ‘Private’ folders.

Yes, sometimes I can use them anywhere too, but I get inconsistent linker errors (sometimes it builds and other times it doesn’t). One time, I figured out that the module causing problems was being a private dependency and switching it to the public got rid of the error permanently.

I’m not 100% certain about what exactly means to have a module as a private or public dependency but I’ve followed that rule since I fixed the error that way.