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.