"No such file or directory exists" How to include header files?

Using Unreal engine 4.10.2 and visual studio community 2015 I can’t figure out how to include a header file into my project so that I can create a static mesh component in my class. If I try and just include “StaticMesh.h”, even though it shows up on the intellisense list, visual studio, when I compile, tells me “No such file or directory exists”. I looked this up and some have suggested to instead include “Engine/StaticMesh.h” instead which I did. However, when I do this my editor crashes and I cannot reopen my project. How is it you are suppose to include files within your C++ classes in UE4? Thanks for any help

It’s good practice to use the full file path. So something like “Runtime/Engine/GameFramework/StaticMesh.h” (that’s just an example path and is probably incorrect.)

The correct way is:

#include "Engine/StaticMesh.h"

But I warn you, you must include anything before the generated line like below

 #include "GameFramework/Pawn.h"
 #include "Engine/StaticMesh.h"
 #include "HandballPawn.generated.h"

It will crash if you do

 #include "GameFramework/Pawn.h"
 #include "HandballPawn.generated.h"
 #include "Engine/StaticMesh.h"

This is the way I was doing it with:

  1. #include “GameFramework/Actor.h”
  2. #include “Engine/StaticMesh.h”
  3. #include “MyClass.generated.h”

However this is crashing my project when I try and add UStaticMeshComponent variable to my code

Okay, I’ll try that once I can figure out what exactly the full path is

Just to point out there, you don’t need to include basic engine types like that.

The path to the source is on my box:

\Epic Games\4.10\Engine\Source\Runtime\Engine\Classes

You can add anything after like

 Runtime/Engine/Classes/Engine/StaticMesh.h

I added a UStaticMeshComponent to my code and it did not crash my editor.
Can you share the way you write your cpp/h files?

Okay, so I’m finding that even if I don’t include a header I can still create a UStaticMeshComponent. I’m guessing it’s within “GameFramework/Actor.h”? However, I noticed that I wasn’t creating a pointer I was just writing:

UStaticMeshComponent PickUpMesh;

and this was crashing my program. Does it make sense that not creating UStaticMeshComponent as a pointer would crash the program?

Okay thank you

Does it make sense that not creating
UStaticMeshComponent as a pointer
would crash the program?

Yes it make sense. Object cannot be shared with the editor, only instance of.

Okay thanks a bunch.

It’s good practice to use the full file path

It’s bad practice to use the full file path. It’s good practice to use the full path from the root of the Include Path. In Unreal, that’s everything after “Public”, “Private”, or “Classes”.

Static Mesh is here:

Engine/Source/Runtime/Engine/Classes/Engine/StaticMesh.h

So you should do:

#include "Engine/StaticMesh.h"

The reason you want to use this path is so your code doesn’t break if Epic moves the Engine module to make StaticMesh’s new path:

Engine/Source/RuntimeCore/Engine/Classes/Engine/StaticMesh.h
or
Engine/Source/Runtime/Engine/Public/Engine/StaticMesh.h
or
Engine/CPlusPlus/EngineModule/Public/Engine/StaticMesh.h

Since Unreal’s compilation system sets the include path root to PublicIncludePaths defined in a Build.cs, you should make your includes relative to that.

Then how do you do it?